I am trying to take an array that contains 400 integers, and split it into a 20×20 2-D array. I thought I had the right algorithm, but the sum of the 1-D array does not match the sum of the 2-D array, so I’m obviously doing something wrong. Here is my code:
private static void processArray(int[] inArray)
{
int[][] array = new int[20][20];
for (int x = 0; x < 20; x++)
{
for (int y = 0; y < 20; y++)
{
for (int z = 0; z < 400; z++)
{
array[x][y] = inArray[z];
}
}
}
}
What am I doing wrong?
You’re assigning the last element in the input array to every element in the output array. The inner loop over the input array must be changed into a single assignment that picks the correct input element.