I'm currently messing around with 2D arrays. I want to Fill a 2D array with a count. I've managed to do this using 2 nested for loops. (That's probably the easiest way of doing it right?)
//create count
int count = 1;
for (int row = 0; row < matrix.GetLength(0); row++)
{
for (int col = 0; col < matrix.GetLength(0); col++)
{
matrix[row, col] = count++;
}
}
I was just curious, is it also possible to fill this 2D array using only a single for loop?
I thought of making a loop that counts the rows. When the rows reaches the end of the array the column wil be increased by 1. This could probably be done by using some if, if else and else statements right?
Does someone here have any idea how to make this work?
matrix.GetLength(0)for both. You should change the second call tomatrix.GetLength(1).int GetArrayValue(int row, int col, int colLength) { return (col * colLength) + row }