Array Assignment Error
Sep 19, 2014 at 3:10am UTC
It appears that the 2D array is assigning two values at once, even though I only give it one value for the row and one for the column. I've programmed in Java before, so I don't know if it has to do with how C++ handles arrays or if it's a compiler error or what.
If you're curious, It's part of a program to make a magic square.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
while (count <= pow(n,2)) {
cout << "Next number to be placed: " << count << "\n" ;
cout << "Row: " << i << "\n" ;
cout << "Column: " << j << "\n" ;
cout << "Initial value at square of i,j: " << square[i][j] << "\n" << "\n" ;
if ( j > side) {
j = 0;
}
if ( i < 0) {
i = side;
}
if (square[i][j] != 0) {
i += 2;
j -= 1;
}
cout << "Updated Row: " << i << "\n" ;
cout << "Updated Column: " << j << "\n" ;
square[i][j] = count;
cout << "New Value: " << square[i][j] << "\n" << "\n" ;
for (int k = 0; k <= side; k++) {
for (int l = 0; l <= side; l++){
cout << square[k][l] << "\t" ;
}
cout << "\n" ;
}
i--;
j++;
count++;
}
And then when I print out the array, it goes from
0 1 0
0 0 0
0 0 2
to
0 1 3
3 0 0
0 0 2
to
0 1 3
3 0 4
4 0 2
but when it gets to 5, it only places one value
0 1 3
3 5 4
4 0 2
I have no idea what is happening, please help!
Sep 19, 2014 at 5:34am UTC
Show how square[][] array is declared and what value of side is.
Topic archived. No new replies allowed.