Printing 100 to 1 ?How? - c#

print 10 number on each line , Below Is The Code Of What I Did But Still Not Successful , Help
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int i;
for (i =100; i>=1; i--)
{
Console.Write(i);
}
Console.ReadLine();
}
}
}

Try this:
for (i = 100; i >=1; i--)
{
if(i%10==0) //if 10 numbers are printed
Console.WriteLine(); //then line break
Console.Write(i+" "); //print the number with a space character
}
Console.ReadLine();
It will print 10 number in each line.
Result:
100 99 98 97 96 95 94 93 92 91
90 89 88 87 86 85 84 83 82 81
80 79 78 77 76 75 74 73 72 71
70 69 68 67 66 65 64 63 62 61
60 59 58 57 56 55 54 53 52 51
50 49 48 47 46 45 44 43 42 41
40 39 38 37 36 35 34 33 32 31
30 29 28 27 26 25 24 23 22 21
20 19 18 17 16 15 14 13 12 11
10 9 8 7 6 5 4 3 2 1
See result in ideone.

Try this
static void Main(string[] args)
{
int i=100;
for (i; i <=100; i--)
{
Console.Write(i);
if(i==0)
{
break;
}
}
Console.ReadLine();
}
Edit
Must use
if(i==0){break;}
otherwise this for loop does not end.

static void Main(string[] args)
{
for (int i = 100; i >0; i--)
{
if (i % 10 == 0)
Console.WriteLine();
Console.Write(i);
}
Console.ReadLine();
}

This might do what you need:
Enumerable.Range(1, 100).Reverse()
.ToList()
.ForEach(i => Console.Write(i % 10 == 1 ? i + "\r\n" : i + ", "));
Which will output:
100, 99, 98, 97, 96, 95, 94, 93, 92, 91
90, 89, 88, 87, 86, 85, 84, 83, 82, 81
80, 79, 78, 77, 76, 75, 74, 73, 72, 71
70, 69, 68, 67, 66, 65, 64, 63, 62, 61
60, 59, 58, 57, 56, 55, 54, 53, 52, 51
50, 49, 48, 47, 46, 45, 44, 43, 42, 41
40, 39, 38, 37, 36, 35, 34, 33, 32, 31
30, 29, 28, 27, 26, 25, 24, 23, 22, 21
20, 19, 18, 17, 16, 15, 14, 13, 12, 11
10, 9, 8, 7, 6, 5, 4, 3, 2, 1

using System;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int i;
for (i = 100; i > 0; i--)
{
if(i%10==0)Console.WriteLine();
Console.Write(i);
}
Console.ReadLine();
}
}
}
Updated Demo:http://ideone.com/st4i8n

Enumerable.Range(0, 10).Reverse().ToList().ForEach(n =>
{
Enumerable.Range(n * 10, 10).Select(i => i + 1).Reverse().ToList().ForEach(i => Console.Write(i + " "));
Console.WriteLine();
});
// output: 100 99 98 97 96 95 94 93 92 91
// 90 89 88 87 86 85 84 83 82 81
// 80 79 78 77 76 75 74 73 72 71
// 70 69 68 67 66 65 64 63 62 61
// 60 59 58 57 56 55 54 53 52 51
// 50 49 48 47 46 45 44 43 42 41
// 40 39 38 37 36 35 34 33 32 31
// 30 29 28 27 26 25 24 23 22 21
// 20 19 18 17 16 15 14 13 12 11
// 10 9 8 7 6 5 4 3 2 1

HundredToOne()
{
for(int i=100;i>0;i--)
{
if(i%10 == 0)
Console.WriteLine("\n");
Console.Write(i);
}
}

You can also use a StringBuilder to build the 10 numbers per line, but concatenating like so should yield sufficient results (since it's only 100 numbers)
foreach (var number in Enumerable.Range(1, 100).ToArray().Reverse())
{
Console.Write((number % 10 == 0) ? "\n" : (number.ToString() + " "));
}

Related

make multiple calls to same async tasks with different data c#

I have a list which contains some documents (for simplicity strings). Now the list is getting populated slowly. What I want to do is when the size of the list reaches 20 I want to call another function that will print these strings asynchronously without stopping the main method. After lot of searching I have managed to put together this code
public void DoStuff()
{
Class1 p = new Class1();
List<string> list = new List<string> { };
var TList = new List<Task>();
int i = 0;
while (i < 90)
{
list.Add(i.ToString());
if (list.Count == 20)
{
Console.WriteLine("List contents when calling: " + list[0]);
TList.Add(Task.Run(() => publishDoc(list)));
list.Clear();
}
i++;
}
if (list.Count != 0)
{
TList.Add(Task.Run(() => publishDoc(list)));
}
Task.WhenAll(TList).Wait();
Console.WriteLine("Done DoStuff");
}
public async Task publishDoc(List<string> docs)
{
Console.WriteLine(iter++ + " " + docs[0]);
await Task.Run(() => Thread.Sleep(1000));
foreach (var val in docs)
{
Console.Write(val + " ");
}
Console.WriteLine();
}
This is what I am getting as output
List contents when calling: 0
List contents when calling: 20
List contents when calling: 40
List contents when calling: 60
1 80
3 80
0 80
2 80
4 80
80 80 80 80 81 82 83 84 85 86 87 88 89
81 82 83 84 85 86 87 88 89
81 82 83 84 85 86 87 88 89
81 82 83 84 85 86 87 88 89
80 81 82 83 84 85 86 87 88 89
Done DoStuff
Done Main
I can't figure out why it is only printing the last passed data i.e. why the passed list is getting overwritten.
Now if I do this
public void DoStuff()
{
Program2 p = new Program2();
List<string> list = new List<string> { };
int i = 0;
while (i < 90)
{
list.Add(i.ToString());
if (list.Count == 20)
{
Console.WriteLine("List contents when calling: " + list[0]);
var tasks = publishDoc(list);
if (tasks.Result == "Done")
{
Console.WriteLine("Done " + list[0]);
}
list.Clear();
}
i++;
}
if (list.Count != 0)
{
var tasks = publishDoc(list);
if (tasks.Result == "Done")
{
Console.WriteLine("Done " + list[0]);
}
}
Console.WriteLine("Done DoStuff");
}
public async Task<string> publishDoc(List<string> docs)
{
Console.WriteLine(iter++ + " " + docs[0]);
foreach (var val in docs)
{
Console.Write(val + " ");
}
Console.WriteLine();
return await Task.Run(() => "Done");
}
I get this output
List contents when calling: 0
0 0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Done 0
List contents when calling: 20
1 20
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
Done 20
List contents when calling: 40
2 40
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
Done 40
List contents when calling: 60
3 60
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
Done 60
4 80
80 81 82 83 84 85 86 87 88 89
Done 80
Done DoStuff
Done Main
This is giving the correct output but is doing it synchronously which I don't want. Please help and thanks in advance.
you have to do like this , as in loop it points to same instance of list every time that might causing issue , so I suggest you create copy of list before passing it to function publishDoc
while (i < 90)
{
list.Add(i.ToString());
if (list.Count == 20)
{
List<string> copy = list.ToList();
Console.WriteLine("List contents when calling: " + copy[0]);
TList.Add(Task.Run(() => publishDoc(copy)));
list.Clear();
}
i++;
}
Refer this answer also : Starting Tasks In foreach Loop Uses Value of Last Item

having formatting error in c#

static void Main(string[] args)
{
OpenFiles(); PrintReportHeadings();
while ((lineIn = fileIn.ReadLine()) != null)
{
ParseLineIn();
PrintDetailLine();
Computing();
sides++;
}
CloseFiles();
}
static void PrintDetailLine()
{
Console.WriteLine("{0,2} {1}", sides, polyName);
}
static void Computing()
{
for (n = 1; n <= 9; n++)
{
p = (Math.Pow(n, 2) * (sides - 2) - n * (sides - 4)) / 2;
Console.Write("{0}", p);
}
}
Here is the code I'm working with. I have my report headings where they need to be and when reading in polygon names from file, they go into the right spot with the amount of sides. But when trying to compute the nth amount of dots in polygonal number, it pushes the amount of sides of polygon and name of polygon to the right side. not sure what I'm doing wrong. been staring at this for 4 hours now.
Is this what you are after?
Output:
3 Triangular 1 3 6 10 15 21 28 36 45
4 Square 1 4 9 16 25 36 49 64 81
5 Pentagonal 1 5 12 22 35 51 70 92 117
6 Hexagonal 1 6 15 28 45 66 91 120 153
7 Heptagonal 1 7 18 34 55 81 112 148 189
8 Octagonal 1 8 21 40 65 96 133 176 225
9 Nonagonal 1 9 24 46 75 111 154 204 261
10 Decagonal 1 10 27 52 85 126 175 232 297
11 Hendecagonal 1 11 30 58 95 141 196 260 333
12 Dodecagonal 1 12 33 64 105 156 217 288 369
13 Tridecagonal 1 13 36 70 115 171 238 316 405
14 Tetradecagonal 1 14 39 76 125 186 259 344 441
15 Pentadecagonal 1 15 42 82 135 201 280 372 477
16 Hexadecagonal 1 16 45 88 145 216 301 400 513
17 Heptadecagonal 1 17 48 94 155 231 322 428 549
18 Octadecagonal 1 18 51 100 165 246 343 456 585
19 Nonadecagonal 1 19 54 106 175 261 364 484 621
20 Icosagonal 1 20 57 112 185 276 385 512 657
21 Icosihenagonal 1 21 60 118 195 291 406 540 693
22 Icosidigonal 1 22 63 124 205 306 427 568 729
23 Icositrigonal 1 23 66 130 215 321 448 596 765
24 Icositetragonal 1 24 69 136 225 336 469 624 801
Press any key to continue . . .
Source Code:
class Program
{
static int sides = 3;
private static int maxPolyNameLength;
static void Main(string[] args)
{
var input = "Triangular Square Pentagonal Hexagonal Heptagonal Octagonal Nonagonal Decagonal Hendecagonal Dodecagonal Tridecagonal Tetradecagonal Pentadecagonal Hexadecagonal Heptadecagonal Octadecagonal Nonadecagonal Icosagonal Icosihenagonal Icosidigonal Icositrigonal Icositetragonal"
.Split(' ');
maxPolyNameLength = input.Max(x => x.Length);
foreach (var s in input)
{
PrintDetailLine(s);
Computing();
Console.WriteLine();
sides++;
}
}
static void PrintDetailLine(string polyName)
{
Console.Write($"{{0,2}} {{1,{maxPolyNameLength}}} ", sides, polyName);
}
static void Computing()
{
double p = 0;
for (var n = 1; n <= 9; n++)
{
p = (Math.Pow(n, 2) * (sides - 2) - n * (sides - 4)) / 2;
Console.Write("{0,3} ", p);
}
}
}

Map array of strings to property in a List of objects C#

I have List of objects List<MyModel> myList
MyModel.cs
public int Id {get; set;}
public int SourceTitle {get; set;}
public int Detail {get; set;}
public string Color {get; set:}
I have an string array of colors ie.
string[] colorPallete[] = {color1, color2, color3, coloe4 ..... color10}
Now, I have about 35 items in my list. There can be multiple items with same SourceTitle and Id. I want to assign a color to the items with the same SourceTitle. Ultimately when the count of my titles goes above 10, the 11th title should be assigned with the Color1 again.
Right now, I do it using Id as below :
myList.ForEach{x => x.Color = colorPallete.ElementAtOrDefault((x.Id -1) % colorPallete.Length));
This works fine, but if the remainder of two different Id's is same, they will be assigned the same color which is not what I want.
Is there a way I can assign the color in relation to the titles and not Ids?
Here is what you want:
SortedList<int, int> colorHistory = new SortedList<int, int>();
int k = -1;
for (int i = 0; i < myList.Count; i++)
{
if (!colorHistory.ContainsKey(myList[i].SourceTitle))
{
colorHistory[myList[i].SourceTitle] = ++k % colorPallete.Length;
}
myList[i].Color = colorPallete[colorHistory[myList[i].SourceTitle]];
}
Output:
ID Source Color
--------------------------
0 5 color1
1 19 color2
2 8 color3
3 15 color4
4 12 color5
5 15 color4
6 3 color6
7 16 color7
8 3 color6
9 17 color8
10 18 color9
11 8 color3
12 4 color10
13 13 color1
14 4 color10
15 18 color9
16 4 color10
17 3 color6
18 14 color2
19 1 color3
20 3 color6
21 4 color10
22 6 color4
23 17 color8
24 13 color1
25 17 color8
26 8 color3
27 6 color4
28 18 color9
29 13 color1
30 2 color5
31 11 color6
32 18 color9
33 19 color2
34 19 color2
35 10 color7
36 13 color1
37 5 color1
38 11 color6
39 2 color5
40 2 color5
41 10 color7
42 12 color5
43 2 color5
44 13 color1
45 12 color5
46 2 color5
47 18 color9
48 14 color2
49 6 color4
50 8 color3
51 11 color6
52 14 color2
53 3 color6
54 7 color8
55 1 color3
56 14 color2
57 10 color7
58 3 color6
59 15 color4
60 7 color8
61 16 color7
62 17 color8
63 10 color7
64 15 color4
65 7 color8
66 10 color7
67 4 color10
68 14 color2
69 13 color1
70 5 color1
71 18 color9
72 12 color5
73 12 color5
74 19 color2
75 12 color5
76 12 color5
77 4 color10
78 5 color1
79 1 color3
80 16 color7
81 10 color7
82 12 color5
83 13 color1
84 5 color1
85 11 color6
86 17 color8
87 16 color7
88 13 color1
89 19 color2
90 15 color4
91 3 color6
92 3 color6
93 15 color4
94 15 color4
95 5 color1
96 13 color1
97 3 color6
98 7 color8
99 6 color4
foreach (var group in list.GroupBy(x => x.SourceTitle))
{
for (int i = 0; i < group.Count(); i++)
{
group.ElementAt(i).Color = palette[i % 10];
}
}
This is what you need:
foreach (var x in myList.GroupBy(m => m.SourceTitle).Select((gms, i) => new { gms, i }))
{
foreach (var m in x.gms)
{
m.Color = colorPallete[x.i % colorPallete.Length];
}
}

Print an array as hex values in c#

I want to print an array as hex. In the following code 'buffer' has been filled by 'XBaseRead'. I can print in hex using a foreach loop and the format "{0:X2}" but when I use string.join method with the same format it prints in decimal.
Here is the code:
byte[] buffer = new byte[160];
XBaseFunctions.XBaseRead(df2500VENDR, buffer, 160, false, XBaseFunctions.O_FLAG._O_BINARY);
foreach (byte i in buffer)
{
Console.Write("{0:X2} ", i);
}
Console.WriteLine("\n");
Console.WriteLine("{0:X2}", string.Join(", ", buffer));
Here is the output:
-----------------Dump of raw buffer-----------------
F0 F0 F0 F0 C1 F0 F1 F0 C1 40 C1 D5 E3 C8 D6 D5 E8 40 50 40 E2 D6 D5 E2 40 C9 D5
C3 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 F1 F4 F5 F0 40 E
6 40 F2 F1 E2 E3 40 E2 E3 5C C5 D9 C9 C5 6B 40 D7 C1 40 40 40 F1 F6 F5 F0 F2 40
40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40
40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 00 00 00 00 00 0
0 00 00 00 00 00 00 F9 60 03 A7 7E 3E 7E 3F 00 00 00 00 00 00 00 00 40 40 40 40
240, 240, 240, 240, 193, 240, 241, 240, 193, 64, 193, 213, 227, 200, 214, 213, 2
32, 64, 80, 64, 226, 214, 213, 226, 64, 201, 213, 195, 64, 64, 64, 64, 64, 64, 6
4, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 241, 244, 245, 240, 64, 2
30, 64, 242, 241, 226, 227, 64, 226, 227, 92, 197, 217, 201, 197, 107, 64, 215,
193, 64, 64, 64, 241, 246, 245, 240, 242, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 249, 96, 3, 167, 126, 62, 126, 63, 0, 0, 0, 0,
0, 0, 0, 0, 64, 64, 64, 64
So the question is Why does the {0:X2} work for the foreach loop but no for the string.join method?
Also, if someone could tell me how to limit each line to 16 bytes then I would appreciate that information too.
You should apply ToString("X2") on every byte first :
Console.WriteLine(string.Join(", ", buffer.Select(b => b.ToString("X2"));
Because when you're doing a foreach loop, you're passing a byte to the formatted Console.WriteLine(). However, you're passing a string of the entire joined buffer in the other instance, as string.Join(", ", buffer) returns a string.
foreach (byte i in buffer)
{
Console.Write("{0:X2} ", i); // <- A byte is being passed.
}
Console.WriteLine("{0:X2}", string.Join(", ", buffer)); // <- A string is being passed.
I would do use this:
var str = string.Join(", ", buffer.Select(x => string.Format("{0:X2}", x)));
Console.WriteLine(str);
...or this:
var str = string.Concat(buffer.Select(i => string.Format("{0:X2}, ", i)))
Console.WriteLine(str);
Although the first one doesn't give a trailing comma, so I would use that over the second method. Thought I'd share the different ways of accomplishing it, though.
Hopefully you're familiar with Linq, so I'm not going to go into great detail as far as what's going on, as it's pretty self-explanatory if you understand Linq.

Read string of numbers into a grid/matrix [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Can anyone tell me how can I read this grid into arrays like a[i][j] ? I searched on google but I can't seem to find anything useful.Thank you very much for helping!
static void Main(string[] args)
{
String grid = "08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08" +
"49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00" +
"81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65" +
"52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91" +
"22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80" +
"24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50" +
"32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70" +
"67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21" +
"24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72" +
"21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95" +
"78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92" +
"16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57" +
"86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58" +
"19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40" +
"04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66" +
"88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69" +
"04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36" +
"20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16" +
"20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54" +
"01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48";
int[] a = new int[20];
for(int i=0;i<20;i++)
for (int j = 1; j < 20; j++)
{
}
}
As suggested in the comments you can simply separate your numbers and use split string. for example:
private static void Main(string[] args)
{
String grid = "08,02,22,97,38,15,00,40,00,75,04,05,07,78,52,12,50,77,91,08," +
"49,49,99,40,17,81,18,57,60,87,17,40,98,43,69,48,04,56,62,00," +
"81,49,31,73,55,79,14,29,93,71,40,67,53,88,30,03,49,13,36,65," +
"52,70,95,23,04,60,11,42,69,24,68,56,01,32,56,71,37,02,36,91," +
"22,31,16,71,51,67,63,89,41,92,36,54,22,40,40,28,66,33,13,80," +
"24,47,32,60,99,03,45,02,44,75,33,53,78,36,84,20,35,17,12,50," +
"32,98,81,28,64,23,67,10,26,38,40,67,59,54,70,66,18,38,64,70," +
"67,26,20,68,02,62,12,20,95,63,94,39,63,08,40,91,66,49,94,21," +
"24,55,58,05,66,73,99,26,97,17,78,78,96,83,14,88,34,89,63,72," +
"21,36,23,09,75,00,76,44,20,45,35,14,00,61,33,97,34,31,33,95," +
"78,17,53,28,22,75,31,67,15,94,03,80,04,62,16,14,09,53,56,92," +
"16,39,05,42,96,35,31,47,55,58,88,24,00,17,54,24,36,29,85,57," +
"86,56,00,48,35,71,89,07,05,44,44,37,44,60,21,58,51,54,17,58," +
"19,80,81,68,05,94,47,69,28,73,92,13,86,52,17,77,04,89,55,40," +
"04,52,08,83,97,35,99,16,07,97,57,32,16,26,26,79,33,27,98,66," +
"88,36,68,87,57,62,20,72,03,46,33,67,46,55,12,32,63,93,53,69," +
"04,42,16,73,38,25,39,11,24,94,72,18,08,46,29,32,40,62,76,36," +
"20,69,36,41,72,30,23,88,34,62,99,69,82,67,59,85,74,04,36,16," +
"20,73,35,29,78,31,90,01,74,31,49,71,48,86,81,16,23,57,05,54," +
"01,70,54,71,83,51,54,69,16,92,33,48,61,43,52,01,89,19,67,48";
var splitstring = grid.Split(',');
var a = new int[20,20];
const int rowCount = 19; //counts 0 as 1
var rowIndex = 0;
var colIndex = 0;
foreach (var s in splitstring)
{
if (rowIndex > rowCount)
{
rowIndex = 0;
colIndex++;
}
a[colIndex, rowIndex] = Int32.Parse(s);
rowIndex++;
}
}
Note the Int32.Parse(s) will throw an exception if the parse fails. You can instead use an Int32.TryParse and use the out value for your result. Depends on what you want to do.
Consider add a space to the end of each "line" such as the following:
String grid = "08 02 .. 91 08 " +
"01 70 .. 67 48 ";
// ^-- add space here
This will allow the string to be converted trivially into a 1D array of strings with string.Split.
string grid = "08 02 .. 91 08"; // every number is space-separated now
string[] gridArray = grid.Split(" "); // -> ["08", "02", .. "91", "08"]
(Even without ensuring the extra spaces, a 1D array can be achieved using a Regular Expression split: var gridArray = Regex.Split(grid, "(?:\s|(?<=\d{2})(?=\d{2}))"), but I recommend "normalizing" the input string literals if possible.)
And each index in the resulting 1D array can be accessed as so, where columns represents the number of columns of the super-imposed matrix, or the "width" of each row.
int columns = 20;
int gridIndex = j * columns + i; // where j is a *row* and i is a *column*
// for a column-major matrix
string numStr = gridArray[gridIndex]; // and value at the [column,row]
Then it's just a matter of converting numStr to an integer and assigning it to the appropriate array index.
If every number is separated by a space such that ever number NN is in form "NN " it also takes up 3 characters. In this case the intermediate Split can be skipped, using the same idea of indexing into the source as a 1D sequence.
int gridNumOffset = (j * columns + i) * 3;
string numStr = grid.Substring(gridNumOffset, 2);
(Finding the substring offset even when there is no space at the end-of-line can be done using a little bit more math, which is a good exercise and the formula just becomes (j * columns + i) * 3 + f(i), where f(i) applies the appropriate offset.)
Another more mundane approach, assuming that the original string cannot be modified to include an end-of-line space/character, is to read in each line as N characters, deal with it, and move on. The concepts from above can be applied:
int rowWidth = (columns * 3) - 1; // -1 assuming no line-end space
for(int j = 0; j < rows; j++) { // j is *rows*, for column-major matrix
string rowStr = str.Substring(j * rowWidth, rowWidth);
string[] row = rowStr.Split(" "); // 1D array just for this row
for (int i = 0; i < columns; i++) {
string numStr = row[i];
// Convert and put value into the correct matrix position
}
}

Categories