Checkboard array won't print properly?

Hi, I have an assignment and part of it is to create a checkerboard array that looks like this:

1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1

This is the code I have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std; 
 
int main() {
int count = 0;
    int checker_board[8][8];
    int x, y;
for (x = 0; x < 8; x++) {
    for (y = 0; y < 8; y++)
    if ((count++ % 2) == 0)
            checker_board[x][y] = '1';
    else
            checker_board[x][y] = '0';
    for (count=0; count<8; count++)
    	printf("%i ", checker_board[x][y]); 
    }
    	printf("\n"); 
    return 0;
}


However, when I try to run it, I get something completely wrong:

42275632 42275632 42275632 42275632 42275632 42275632 42275632 42275632 1 1 1 1 1 1 1 1 4253648 4253648 4253648 4253648 4253648 4253648 4253648 4253648 40 40 40 40 40 40 40 40 8 8 8 8 8 8 8 8 42275632 42275632 42275632 42275632 42275632 42275632 42275632 42275632 1 1 1 1 1 1 1 1 42275744 42275744 42275744 42275744 42275744 42275744 42275744 42275744

I've spent a frustratingly long amount of time on this code and still have gotten nowhere. Can anyone help?
Lack of indentation and the lack of of { } (block braces) are what is tripping you up the most, I think.

This is what your current code looks like, with "proper" indentation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std; 
 
int main() {
    int count = 0;
    int checker_board[8][8];
    int x, y;
    for (x = 0; x < 8; x++) {
        
        for (y = 0; y < 8; y++)
            if ((count++ % 2) == 0)
                checker_board[x][y] = '1';
            else
                checker_board[x][y] = '0';
                
        for (count=0; count<8; count++)
    	    printf("%i ", checker_board[x][y]); 
    }
    
    printf("\n"); 
    return 0;
}


When you print your board, you're trying to access checker_board[x][8], which is out of bounds.

_____________________________________

I suggest: Keeping the generation of your checker_board separate from the display logic of your board:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std; 
 
int main() {

    char checker_board[8][8];
    
    for (int x = 0; x < 8; x++) {
        
        for (int y = 0; y < 8; y++)
        {
            if (((x+y) % 2) == 0)
                checker_board[x][y] = '1';
            else
                checker_board[x][y] = '0';
        }
    }
    
    for (int x = 0; x < 8; x++){
        for (int y = 0; y < 8; y++) {
            printf("%c ", checker_board[x][y]); 
        }
        printf("\n"); 
    }
    return 0;
}


Also:
- Keep variables in the inner-most scope as possible (your x and y).
- use %c for chars.

___________________________________________

Also also:

Since you're clearly using C++, why not use modern C++ I/O syntax?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

int main()
{
    using std::cout;
    using std::endl;

    char checker_board[8][8];
    
    for (int x = 0; x < 8; x++) {
        
        for (int y = 0; y < 8; y++) {
            
            if ((x+y) % 2 == 0)
                checker_board[x][y] = '1';
            else
                checker_board[x][y] = '0';
        }
    }
    
    for (int x = 0; x < 8; x++) {
        for (int y = 0; y < 8; y++) {
            cout << checker_board[x][y] << " ";
        }
        cout << "\n";
    }
    return 0;
}
Last edited on
Topic archived. No new replies allowed.