Jump to content

  • Log In with Google      Sign In   
  • Create Account


(C++) Printing Out 2d Array


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
17 replies to this topic

#1 Youbar   Members   -  Reputation: 122

Like
1Likes
Like

Posted 06 December 2012 - 06:15 PM


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.

Sponsor:

#2 ultramailman   Prime Members   -  Reputation: 1557

Like
0Likes
Like

Posted 06 December 2012 - 06:30 PM

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.

#3 Youbar   Members   -  Reputation: 122

Like
0Likes
Like

Posted 06 December 2012 - 06:32 PM

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;
}


#4 Álvaro   Crossbones+   -  Reputation: 11881

Like
4Likes
Like

Posted 06 December 2012 - 06:34 PM

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';
}


#5 Youbar   Members   -  Reputation: 122

Like
0Likes
Like

Posted 06 December 2012 - 06:49 PM

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

Edited by Youbar, 06 December 2012 - 06:51 PM.


#6 Álvaro   Crossbones+   -  Reputation: 11881

Like
4Likes
Like

Posted 06 December 2012 - 07:11 PM

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.

#7 Youbar   Members   -  Reputation: 122

Like
0Likes
Like

Posted 06 December 2012 - 08:41 PM

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

#8 MarkS   Prime Members   -  Reputation: 875

Like
2Likes
Like

Posted 06 December 2012 - 09:05 PM

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:


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

}



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, 06 December 2012 - 09:07 PM.


#9 Youbar   Members   -  Reputation: 122

Like
0Likes
Like

Posted 06 December 2012 - 09:20 PM

Oh, I understand now, thanks. Posted Image

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, 06 December 2012 - 09:27 PM.


#10 MarkS   Prime Members   -  Reputation: 875

Like
2Likes
Like

Posted 06 December 2012 - 09:25 PM

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.


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();

	 }

}


Edited by MarkS, 06 December 2012 - 09:56 PM.


#11 Youbar   Members   -  Reputation: 122

Like
0Likes
Like

Posted 06 December 2012 - 09:38 PM

Thanks. 1+ rep for you again. :)

#12 MarkS   Prime Members   -  Reputation: 875

Like
1Likes
Like

Posted 07 December 2012 - 11:42 AM

If you really want to fry your noodle, map can also be referenced as "map[(y * 5) + x] = ...".Posted Image

#13 ultramailman   Prime Members   -  Reputation: 1557

Like
0Likes
Like

Posted 07 December 2012 - 02:59 PM

If you really want to fry your noodle, map can also be referenced as "map[(y * 5) + x] = ...".Posted Image

But that would require casting to int* first, otherwise it will access the row (y * 5) + x.

#14 Álvaro   Crossbones+   -  Reputation: 11881

Like
3Likes
Like

Posted 07 December 2012 - 03:09 PM

If you really want to fry your noodle, you'll print your array like this:
for (int i=0; i<25; ++i) std::cout << i[*map] << "	\n"[i%5];
Posted Image

Edited by Álvaro, 07 December 2012 - 03:11 PM.


#15 Youbar   Members   -  Reputation: 122

Like
0Likes
Like

Posted 07 December 2012 - 03:53 PM

How would those lines work?
map[(y * 5) + x] = ...
seems like y is pointing to 5, which would just slow things down by the looks of it.
And:
for (int i=0; i<25; ++i) std::cout << i[*map] << " \n"[i%5];
How does i[*map] work?
i points to map?

#16 Álvaro   Crossbones+   -  Reputation: 11881

Like
1Likes
Like

Posted 07 December 2012 - 04:13 PM

Unless operator[] has been overridden, a[b] is really short-hand notation for *(a+b). Typically a is a pointer and b is an integer, but there is nothing preventing you from reversing the order. Now *map is a pointer to the first row of the matrix, and I am abusing it to access the whole matrix.

The " \n"[i%5] is a bit more straight forward: It evaluates to a space unless i%5==4, which is when you need to print a '\n'. So it separates elements in a row and prints new-line characters all in one little expression.

#17 MarkS   Prime Members   -  Reputation: 875

Like
0Likes
Like

Posted 08 December 2012 - 10:13 AM


If you really want to fry your noodle, map can also be referenced as "map[(y * 5) + x] = ...".Posted Image

But that would require casting to int* first, otherwise it will access the row (y * 5) + x.


DOH! And there I was trying to look smart...Posted Image

#18 ultramailman   Prime Members   -  Reputation: 1557

Like
1Likes
Like

Posted 08 December 2012 - 02:02 PM



If you really want to fry your noodle, map can also be referenced as "map[(y * 5) + x] = ...".Posted Image

But that would require casting to int* first, otherwise it will access the row (y * 5) + x.


DOH! And there I was trying to look smart...Posted Image

Heh, don't we all? My first post in this thread was also a failed attempt :o




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS