Jump to content
  • Advertisement
Sign in to follow this  
Youbar

(C++) Printing Out 2d Array

This topic is 2171 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts



int map[5][5] =
{
1, 1, 1, 1, 1,
1, 0, 0, 0, 1,
1, 0, 2, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 1
};

int column = 0;
int column2 = 0;

()void print_map()
void print_map()
{
for (column = 0; column < 6; column++)
{
cout << map[column][column2];
if (column == 5)
{
column2++;
column = 0;
cout << endl;
}
if (column && column2 == 5)
{
break;
}
}
}

This is my code for printing out a 2d array.
What happens in the output though, is unexpected. It produces:

111110
00010
02010
00010
11110
1

Instead of:
11111
10001
10201
10001
11111

1. How can I fix this problem?
2. Can you call the an if command an exception?
3. Please don't post the fixed code. Just give me hints, unless the code is beyond fixing.

Share this post


Link to post
Share on other sites
Advertisement
What's with the column < 6 ? There are only 5 columns, so it should be column < 5. Remember, the index of the first element of an array is zero, and the index of the last element is number of elements - 1.

Share this post


Link to post
Share on other sites
That's to keep the loop going on. It's broken when Column and Column2 are both equal to 5.

if (column && column2 == 5)
{
break;
}

Share this post


Link to post
Share on other sites
You could increment column before the `if (column==5)...' instead of after. But the easiest solution is to use nested loops.
for (row = 0; row < 5; ++row) {
for (column = 0; column < 5; ++column)
cout << map[row][column] << ' ';
cout << '\n';
}

Share this post


Link to post
Share on other sites
By the looks of it, the code would print the array diagonally.
Can you please explain how that loop would work? Edited by Youbar

Share this post


Link to post
Share on other sites

By the looks of it, the code would print the array diagonally.
Can you please explain how that loop would work?


I don't know what part of it you don't know. A for loop has four elements:
for (initialization; condition; update) {
body;
}


`initialization' is run first, then `condition' is checked. If it is true, the body is executed, then the update, and then the condition is checked again. We keep going like that until the condition is false.

You can think of it as being equivalent to this, which is perhaps easier to follow:
initialization;
while (condition) {
body;
update;
}


Now go carefully through my code and see what it would do.

Share this post


Link to post
Share on other sites
What I mean, is how does that print out all of the array? It looks as if it would just print out the array like this:
1
0
2
0
1

Share this post


Link to post
Share on other sites
To expand on what Alvaro said, nested loops do not run concurrently. The "column" for loop completes before control is given back to the "row" for loop. Another way of looking at that code snippet is:

[source]
for (row = 0; row < 5; ++row) {
cout << map[row][0] << ' '; // print cell data at [row][0], followed by a space.
cout << map[row][1] << ' '; // print cell data at [row][1], followed by a space.
cout << map[row][2] << ' '; // print cell data at [row][2], followed by a space.
cout << map[row][3] << ' '; // print cell data at [row][3], followed by a space.
cout << map[row][4] << ' '; // print cell data at [row][4], followed by a space.
cout << '\n'; // print a new line character
}
[/source]

The five columns for the first row (row 0) are printed in sequence, followed by a new line character and then "row" is incremented by one and the next five columns for that row are printed and so on. Edited by MarkS

Share this post


Link to post
Share on other sites
Oh, I understand now, thanks. smile.png

EDIT:
I'm getting an error in this code:


#include <iostream>
using namespace std;
int map[5][5] =
{
1, 1, 3, 1, 1,
1, 0, 0, 0, 1,
1, 0, 2, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 1
};
int column = 0;
int row = 0;
int x = 2;
int y = 2;
char dir = 'x';
bool game_ended = false;
void stop()
{
cout << endl;
system("PAUSE");
}
void cls()
{
system("CLS");
}
void print_map()
{
for (row = 0; row < 5; ++row)
{
for (column = 0; column < 5; ++column)
{
cout << map[row][column] << ' ';
}
cout << endl;
}
}
void redraw()
{
cls();
print_map();
}
int main()
{
print_map();
cout << "What direction would you like to go in?" << endl;
cin >> dir;
while (game_ended != true)
{
if (dir == 'w')
{
map[x][y] = 0;
x--;
map[x][y] = 2;
redraw();
}
}
stop();
}


I get an error that says:

Unhandled exception at 0x00411ced in Turn-based RPG.exe: 0xC0000005: Access violation writing location 0x0041aff4.

I am guessing this is due to the array. How would I fix this? Edited by Youbar

Share this post


Link to post
Share on other sites
Glad I could help.

[edit]

The error you're getting is because you start "x" at 2 and deincrement it without any form of control to keep it from dropping bellow 0.

You need to do bounds checking. "x" and "y" are bounded to 0..4, inclusive.

[source]
while (game_ended != true)
{
if (dir == 'w')
{
map[x][y] = 0;
if(x > 0) // Only deincrement 'x' if x > 0. If x == 0, "x--;" is skipped and the array doesn't go out of bounds.
{
x--;
map[x][y] = 2;
}
redraw();
}
if (dir == 'e') // I'm making an assumption here to make a point....
{
map[x][y] = 0;
if(x < 4) // Only increment 'x' if x < 4. If x == 4, "x++;" is skipped and the array doesn't go out of bounds.
{
x++;
map[x][y] = 2;
}
redraw();
}
}
[/source] Edited by MarkS

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!