Question

I'm trying to create an array with the following structure

D
C C
B B B
A A A A

Which will be N x N but split diagonally. Initially, all I know is the bottom row essentially just:

string[,] table = new string[n,];

How can I build on this structure so that when I get to the next row I can declare how many elements it has? Would it be something along the lines of:

for(int i = 0; i <= n; i++) {
    table[i] = new string[--n]
}
Was it helpful?

Solution

The [,] syntax creates multidimensional arrays, not jagged arrays. For the latter, you would need to do this:

int n = 4;
string[][] table = new string[n][];
for (int i = 0; i < n; i++)
    table[i] = new string[n-i];

Btw. you really don’t want to decrement n while looping in a loop with the condition i < n (or i <= n).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top