2d Array Reverse Error

Pages: 12
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.
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
37
38
39
40
41
42
43
44
45
46
47
48
// 2d Array Reverse.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	int board[3][3];
	int width=3;
	int height=3;
	for(int z=0;z<3;z++)
	{
		for(int e=0;e<3;e++)
		{
		cout<< "Enter number";
		cin>> board[z][e];
		}
	}
	cout<< "Before"<<endl;
	for(int i = 0; i<width; i++)
	{
		for(int j=0; j<height; j++)
		{
			cout<< board[i][j] << " ";
		}
		cout<< endl;
	}
	cout<< "After"<<endl;
	int first1 = 0;
	int last1 = 9;
	for(int a=0;a<width;a++)
	{
		
		for(int b=0;b<width; b++)
		{
			swap(board[0][first1],board[0][last1]);
			cout<< board[a][b] << " ";	
			first1++;
			last1--;
		}
		cout<< endl;
	}
	return 0;


}
Last edited on
What are you trying to do with line 45? It's a 2d array, so it's not clear what a single subscript means in terms of row and column.


I updated the code... still doesn't work. This time it ouputs:
[garbage value] 2 [garbage value]
4 5 6
7 8 9
for input
1,2,3,4,5,6,7,8,9
lines 14-32 could be put in a for loop.
I changed my code again now it gives me this
Error no instance of overloaded function swap matches the argument list
Now I get this error: Unhandled exception at 0x00000001 in 2d Array Reverse.exe: 0xC0000005: Access violation executing location 0x00000001
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]
board[end] doesn't make sense for a 2d array, and 9 wouldn't be a valid element number.

Last edited on
[9][8][7]
[6][5][4]
[3][2][1]
wildblue I updated the code did you see?
last = 3 the largest you want is 2
you're incrementing first and last, but then they get reinitialized when the for loop starts again
Now it gives me
923
456
781
So how can i do this?
It looks like these are the position swaps you want to do.

[0][0] -> [2][2]
[0][1] -> [2][1]
[0][2] -> [2][0]

[1][0] -> [1][2]
[1][2] -> [1][0]
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.
Last edited on
Code updated how can I fix this it only swaps first and last.
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.


[0][0] -> [0,8]
[0][1] -> [0,7]
[0][2] -> [0,6]
[0][3] -> [0,5]
My code crashes with the same error: Run Time Check Failure #2
The element numbers run from 0 to 8, so 9 is out of bounds for the last variable.

You no longer need a nested loop for the swap. And the for loop only needs to run 4 times.

Edit: I'd pull the output out of the swap loop and just have a separate loop to output after (like the before loop)
Last edited on
Pages: 12