Write your question here.
Hey everyone, my program is supposed to take 9 numbers as an input then reverse it into a 2d array. I can't do that because it's telling me Error: Expression must be a modifiable value at line 45. Is there any other way to do this? Thanks.
How does one reverse a 2d array? I can understand a 1d array:
[1][2][3][4][5] -> [5][4][3][2][1]
but how is a 2d array supposed to look like when it's reversed?
[1][2][3]
[4][5][6] -> ?
[7][8][9]
I hope this doesn't make things more complicated, but there's an interesting little trick you can use with 2d arrays.
say we have int ary[3][3];
It looks like this to us:
[1][2][3]
[4][5][6]
[7][8][9]
but to the computer, it looks like this:
[1][2][3][4][5][6][7][8][9]
The first index of this array is ary[0][0], and the last index is ary[2][2].
If we think of the array as the computer does in one dimension, the last index can also be accessed by ary[0][8].
If you do it this way, you only have to loop through one variable instead of two.
I'd follow Yay295's hint to flatten the array. Then you won't need a nested for loop. Don't reinitialize the variable in the for loop - it undoes whatever increment or decrement you've done.