Hey guys and girls,
Sorry if this has been asked a thousand times before but I've searched and not found an answer so thought I'd ask in case others are having the same problem (but mostly cause I am and I can't figure it out!)
I want to return a 2D int array from a function, and that's my first mistake! I see C doesn't like this so I've changed it to a Vector<Vector<int> > but being a stubborn Java-lover I'm still manipulating it like a 2D array. As far as I am aware this is acceptable and even appears to be working except that the values in each 1st dimension are just repeated over and over (I'm considering the 1st dimension to be the left one in the [][] pair).
In case my explanation doesn't make sense here is a little code and the resultant output:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
Vector<Vector<int> > gameState (10, Vector<int> (10));
for (int row = 0; row < 10; row++) {
for (int column = 0; column < 10; column++) {
gameState[row][column] = (column * 10) + row;
}
}
cout << "Game state:\n";
for (int x = 0; x < 10; x++) {
for (int y = 0; y < 10; y++) {
cout << gameState[x][y] << " ";
}
cout << "\n";
}
|
And the output is.... well, you tell me ;) From my reckoning it should be
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
... ...
but it's not.
Ok, here is the actual output:
Game state:
9 19 29 39 49 59 69 79 89 99
9 19 29 39 49 59 69 79 89 99
9 19 29 39 49 59 69 79 89 99
9 19 29 39 49 59 69 79 89 99
9 19 29 39 49 59 69 79 89 99
9 19 29 39 49 59 69 79 89 99
9 19 29 39 49 59 69 79 89 99
9 19 29 39 49 59 69 79 89 99
9 19 29 39 49 59 69 79 89 99
9 19 29 39 49 59 69 79 89 99
Strange, no?