Displaying 2d Array contents like a Graph (coordinates)

I'm having difficulty displaying the contents of a 2D array like a graph's coordinate system - i.e as standard, [0][0] is displayed in the top left of the output, but I want [0][0] to be displayed at the bottom left.

I have two nested for loops to output the contents of the 2d array to the screen, and I tried reversing the criteria but that puts [0][0] in the bottom right. I think what I need is a way to display a "mirror image" of the default output.

Anyone have any ideas/suggestions?

Cheers,
S
Last edited on
You need to make your outer for() loop go from row N-1 down to 0 and your inner for() loop go from column 0 up to M-1.

Your first attempt was 0...N-1 and 0...M-1.
Your second attempt was N-1...0 and M-1...0.

Thanks for the reply. I understand what you mean but it doesnt seem to be working. I'll post the code here:


#include <iostream>

using namespace std;

int main() {

char myArray[4][4];

for (int i = 0; i < 4; i ++) {
for (int j = 0; j < 4; j++) {
myArray[i][j] = '.';
cout << myArray[i][j];
if (j == 3)
cout << endl;
}
}

myArray[0][0] = 'Q';

for (int i = 4; i > 0; i--) {
for (int j = 0; j < 4; j++) {
cout << myArray[i][j];
if (j == 3)
cout << endl;
}
}

return 0;
}

The first output is as desired:

....
....
....
....

But the 2nd input gives me an ASCII symbol for the first line followed by three lines of '.' and there is no sign of the 'Q' at the bottom left location.

Any ideas?

Cheers,
S
Watch out.....u're going out of the limits of the array
1
2
3
4
5
6
7
8
for (int i = 4; i > 0; i--) {
for (int j = 0; j < 4; j++) {
cout << myArray[i][j];
if (j == 3)
cout << endl;
}
}


it really should be for(i=3;i>=0;i--)...


And by the way:
1
2
3
4
5
6
7
8
for (int i = 0; i < 4; i ++) {
for (int j = 0; j < 4; j++) {
myArray[i][j] = '.';
cout << myArray[i][j];
if (j == 3)
cout << endl;
}
}
U do't need to use that IF.....it's alright if u just do like this:
1
2
3
4
5
6
7
8
9
for(int i=0;i<4;i++)
{
        for(int j=0;j<4;j++)
        {
                 myArray[i][j]='.';
                 cout<<myArray[i][j];
         }
         cout<<endl;
}
Last edited on
That worked. Thanks for the reminder about limits and the cout tip. Much appreciated!

S
Topic archived. No new replies allowed.