Public Group

# for next loop for 2d arrays (C++)

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

## Recommended Posts

this is a pretty simple array problem. i Need to loop through a d2 array vb code would be like this for x = 0 to 2 for y = 0 to 2 picbox.print board(x,y) next y next x where board is a 2d array i want to do the same thing in c++ for a tic tac toe game dos -style :P could ne1 help me out with loopin through the elements. I had somethin that appeared to work but the positions weren't rite. thnx

##### Share on other sites
for(int i = 0; i < 3; i++){  for(int j = 0; j < 3; j++)  {    cout << board[j] << endl;  }}

edit: tic-tac-toe has three rows/columns as iMalc pointed out

[Edited by - Shai on January 6, 2005 4:41:54 PM]

##### Share on other sites
The C++ for loop has 3 parts to it:

the initializer: where your variables are initialized
the condition: when the loop should stop
and variable modification: upon the completion of each loop, this happens.

So this is what a for loop looks like:
for(part1;part2;part3){   what you want inside}

So what you are looking for is:
for(int x=0; x < 2; x++){   for(int y=0; y < 2; y++){      cout << board[x][y];   }   cout << endl;}

What you should realize, however, is that 2d arrays are accessed [y][x], so [0][1] is the first row, second column (remember, it indexes at 0). Be careful of this when you are trying to print something out on the screen from an array! If you want to print everything in the same row at once, you need to loop y, then loop x. If you look at the code example I gave you, [x][y] is how we are used to seeing things, so it is set up that way. However, in our example, the y variable is actually being used to access columns, instead of rows like y normally signifies. Perhaps a tidbit of info too much, but it never hurts to know!

-visage
Captain of Misiniformation.

##### Share on other sites
for (int x = 0;x<3;x++)
{
for (int y = 0;y<3;y++)
{
cout << board[x,y]; // it could be board[x][y]; i cant remember
}
}
That should work.

3 people beat me to it. lol.

##### Share on other sites
Quote:
 Original post by crazy_andyfor (int x = 0;x<3;x++){ for (int y = 0;y<3;y++) { cout << board[x,y]; // it could be board[x][y]; i cant remember }}That should work.3 people beat me to it. lol.
Actually you were the first person to realise that tic-tac-toe borads have 3 rows and columns. The other posts had loops going from 0 to 1. (Brainfart)

(Yes it's board[x][y] btw)

I also just thought I'd make it clear that with 'part2' (the condition) it is actually the condition for the loop to execute. i.e. the loop stops only when this condition is false. The condition is evaluated before each iteration of the loop too. So this correctly outputs nothing:
int n=0;for (int i=0; i<n; ++i)  cout << i << endl;

Also, you can use 'for' loops for like a million different things. e.g.
//a linked list traversal:for (Node *curr=head; curr!=NULL; curr=curr->next){  cout << curr->val << endl;}//an infinite loopfor (;;){  if (user_pressed_esc()) break;}...

Happy coding!

##### Share on other sites
thats the code i was tryin to use but i have a problem. When i say put first column third row it does that one but then it puts a "x" in 0,1 here is the output

0x0
000
x00

that should not happen. Well here is my code.

#include
using namespace std;

int main()
{

char board[2][2];

for(int x=0; x < 3; x++){
for(int y=0; y < 3; y++){
board[y][x] = '0';
}

}

board[0][2]='x';

for(int x=0; x < 3; x++){
for(int y=0; y < 3; y++){
cout << board[y][x];
}
cout << endl;
}
return 0;
}

i would appreciate if some1 can help
thnx

##### Share on other sites
for(int x=0; x < 3; x++){
for(int y=0; y < 3; y++){
cout << board[y][x];
}
cout << endl;
}
return 0;
}

that prints the y values acrosds and the x values down

for(int y=0; y < 3; y++){
for(int x=0; x < 3; x++){
cout << board[y][x];
}
cout << endl;
}
return 0;
}

try that.

Quote:
 (Brainfart)

:D

##### Share on other sites
There is a problem in the board definition:
Quote:
 char board[2][2];
The numbers in the brackets are not the maximum value, but are the number of values i.e. they should both be 3 so that they will hold 0, 1, and 2 (3 values).

Andy is right too. (we cross posted) the for loops need to be put in the other order.
[/edit]

##### Share on other sites
Quote:
Original post by iMalc
The problem is in the board definition:
Quote:
 char board[2][2];
The numbers in the brackets are not the maximum value, but are the number of values i.e. they should both be 3 so that they will hold 0, 1, and 2 (3 values).

Andy is right too. (we cross posted) the for loops need to be put in the other order.[/edit]

didn't notice that one.

##### Share on other sites
well this code seems to be working.

char board[3][3];
for(int x=0; x < 3; x++)
{
for(int y=0; y < 3; y++)
{
board[y][x] = 1;
board[0][2] = 'x';
cout << board[y][x];
}
cout << endl;
}

thnx

1. 1
2. 2
frob
12
3. 3
4. 4
5. 5
Rutin
10

• 13
• 14
• 65
• 14
• 15
• ### Forum Statistics

• Total Topics
632130
• Total Posts
3004283

×