creating a table

hello, i'm having a problem with printing an array
i would like to print a 2d array in the format
000
000
000
and this is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main(){

int board[3][3];
int i,j;

for(j=0;j<3;j++){
	for(i=0;i<3;i++){
		board[i][j] =0;
		cout << board[i][j];
		if(i=3){cout << "\n";}
		}
		}
 return 0;
 }


but all i get back is

0
0
0

can anyone help me? thanks for any replies.
if(i=3){cout << "\n";}
change to:
if(i==3){cout << "\n";}
hmm, after add that bit and changing something else i get

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main(){

int board[3][3];
int i,j;

for(j=0;j<3;j++){
	for(i=0;i<3;i++){
		board[j][i] =0;
		cout << board[j][i];
		if(i==3){cout << "\n";}
		else cout << " ";
		}
		}
 return 0;
 }


but all that returns is
0 0 0 0 0 0 0 0 0
strange.
Last edited on
fixed it. the right code to get my grid is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int main(){

int board[3][3];
int i,j;

for(j=0;j<3;j++){
cout << "\n";//i needed this to put in line breaks
	for(i=0;i<3;i++){
		board[j][i] =0;
		cout << board[j][i];
		if(i!=3){cout << " ";}
			}
			
		}
 return 0;
 }

thanks for the help
sorry
it should have been this:
if(i==2){cout << "\n";}
element 3 does not exist it's [0][0], [0][1], [0][2]. \n [1][0], [1][1], [1][2]. \n [2][0], [2][1], [2][2].
Last edited on
the code above does work and prints
000
000
000
so its fine for me.
Do you see what you did wrong though?
the first time you were not comparing your were assigning, the second it was comparing element 3, which didn't exist so the use of if(i==2){cout << "\n";} should work exactly as you wanted, however you found a way around that by putting the newline in the outer loop.
Topic archived. No new replies allowed.