Reputation: 57
I am currently working on a dungeon crawl game where the user can move throughout a maze on the screen. I decided to use a 2d array for the maze. One problem, I have a function to print the maze although its not working. I want it to print all four of the rows (there are supposed to be 4 0's per row) but it only prints 4 0's in a single line.
int maze[4][4] = {(0,0,0,0),
(0,0,0,0),
(0,0,0,0),
(0,0,0,0)};
for (int i = 0; i < 4; i++)
{
cout <<maze[i][i];
}
Upvotes: 0
Views: 111
Reputation: 399
You need nested loop for displaying 2D array. for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { cout<
Upvotes: 1
Reputation: 186
Try this.
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
cout <<maze[i][j];
}
cout << "\n";
}
Upvotes: 0
Reputation: 20620
You need two loops, one nested inside the other.
One to print the rows.
One to print each column in the current row.
Upvotes: 2