Print a checkerboard (8-by-8 grid).

So I'm trying to work through a c++ book and I'm stuck on one of the exercises.

Exercise 8-1: Print a checkerboard (8-by-8 grid). Each square should be 5-by-3 characters wide. A 2-by-2 example follows
1
2
3
4
5
6
7
8
9
+-----+-----+ 
|     |     | 
|     |     | 
|     |     | 
+-----+-----+ 
|     |     | 
|     |     | 
|     |     | 
+-----+-----+ 


This is what I tried to do:

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Make a checkerboard - 8x8
//April 16, 2011

#include <iostream>
#include <string>

using namespace std;

int main()
{

	const int width = 48;
	const int height = 32;

	char board[width][height];

int i = 0;
int j = 0;

	   for (i=0; i<=width; ++i)
	  {
        for(j=0; j<=height; ++j)        
		{
                if (i % 6 == 0 || j % 4 == 0)
			cout <<  "+";

		if (i % 6 > 0  || j % 4 == 0)
			cout << "-";

		if (i % 6 == 0 || j % 4 > 0)
			cout << "|";

		if (1 % 6 > 0 || j % 4 > 0)
			cout << " ";		
                }
              cout <<"\n";
	  }

	cout << "\nPress enter to exit";
	cin.ignore(cin.rdbuf()->in_avail() + 1);

	return 0;
}


The result is ugly and not at all what I am trying to do. I know I still have a lot to learn so any help would be appreciated.
Does this work for you:

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
29
30
31
32
33
// Make a checkerboard - 8x8
//April 16, 2011

#include <iostream>
#include <string>

using namespace std;

int main()
{

    const int rows = 5;
	const int columns = 5;
	char board [rows][columns] =
	{
		{'X', 'X', 'X', 'X', 'X',},
		{'X', ' ', 'X', ' ', 'X',},
		{'X', ' ', 'X', ' ', 'X',},
		{'X', ' ', 'X', ' ', 'X',},
		{'X', 'X', 'X', 'X', 'x',}

	};

	for (int i = 0; i < rows; i++)
		{
			for (int j = 0; j < columns; j++)
			{
				cout << board[i][j];
			}
			cout << endl;
		}

}



Or if you want a regular "Print"

1
2
3
4
cout << "XXXXXXX"
cout << "X  X  X"
cout << "X  X  X"
cout << "XXXXXXX"
Last edited on
As a start, two points:

1. You need an extra element in each array. The first six columns complete the first square without including the right side vertical column which starts the next column. Similarly after 48 columns you will still need the final vertical column. Look at your two by two example grid and note that it contains 13 characters not 12.
The same thing will happen in the final row at the bottom of the grid so change lines 12 and 13 to:

1
2
   const int width = 49;
   const int height = 33;


Your board array will now be large enough to hold the complete grid. Note also that you never use board.

2. Your outer loop iterates over the width (the columns) and the inner loop over the height (the rows). This means you are trying to start with column zero and output all the rows, then go to column one and out all of its rows etc. This cannot possibly work. Instead you need to start with the top row in the outer loop and in the inner loop iterate over the columns so you are outputting a row at a time. You need to reverse the order of your loops:

1
2
3
4
5
6
for(j=0; j<height; ++j) 
{
    for (i=0; i<width; ++i)
    {
        .
        .


Also note that I have changed <= to < in the loop conditions. Without this change your code would go through the loop height + 1 and width + 1 times because you start your loop variable at zero.

All of the inner logic will have to be reworked.
Topic archived. No new replies allowed.