Create a "checkerboard" using cin

I am stuck on a problem due this week. Here is the description of the problem: "A generalized checkerboard is a rectangular grid one that has four parameters:

R: The number of rows.
C: The number of columns.
SC: The starting character.
CS: The cycle size.

The grid is a (R * C) matrix, where the element in row r and column c, contains the character SC + (r+c)%CS. For example, here's a generalized checkerboard with R=5, C=6, SC = 'a' and CS=4:

a b c d a b
b c d a b c
c d a b c d
d a b c d a
a b c d a b

Your job is to write the program checkerboard.cpp, which reads five integers from standard input. The first four are the parameters R, C, SC and CS, as defined above. The fifth parameter is a width W. Your program should print out the specified checkerboard such that each element of the grid is printed as a (W * W) square."

Sorry I know that is a bit long. My issue is I am not certain of the best way to go about this. I've spent at least 5 hours on this and all I have managed to get is the code I've shown below. I'm not even sure if nesting for loops is the way to go about it. My instructor says we shouldn't have to use any arrays or vectors, just a good grasp of cin. Can anybody provide some assistance so I can finally wrap my head around this without actually giving me the algorithm (I do wish to learn, after all).

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
  #include <iostream>
#include <vector>

using namespace std;

int main(){
	
	int R, C, CS, W; // Rows, collumns, Cycle-Size, Square-Size
	char SC; // Starting-char
	
	cin >> R >> C >> SC >> CS >> W;
	
	if (R <=0 || C <= 0 || CS <= 0 || W <= 0) exit(0);
	
	if (!(cin >> R >> C >> SC >> CS >> W)){exit(0);}
	if (SC + '0' + CS >= 127) exit(0);
	
	for (int i = 0; i < C; i++){ // For loop for collumns
		for (int j = 0; j < R; j++){ // For loop for rows
			for (int k = 0; k < CS; k++){ // For loop for the cycle size
				for (int w = 0; w < W; w++){ // For loop for W
					cout << SC ;
					}
				} 
			SC += 1;
			
			}
		
		}
	
	cout << endl;
	
	
	
	
	}// End of Main 
All you need is to implement the formula given in the text. You need only two loops, the inner loop 0<=c<C, the outer loop 0<=r<R. In the inner loop print SC + (r+c)%CS. Make sure you transform it into char, otherwise will print integer. In teh outer loop just print a new line character.
By the way, what's the use of W? I did not see that anywhere in the text. You don't have a square.
Sorry I forgot to specify! If W happened to be 2 then sample output might look like:

AABBCCDD
AABBCCDD


In this the columns would be 4, rows would be 1, cycle size would be 4, because it's only from A - D. What W does is one "unit" would be AA.
AA

In other words every "square" of the "checkerboard" would be (W*W)


Also this is what I have so far:

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
#include <iostream>
#include <vector>

using namespace std;

int main(){
	
	int R, C, CS, W; // Rows, collumns, Cycle-Size, Square-Size
	char SC; // Starting-char
	
	cin >> R >> C >> SC >> CS >> W;
	
	if (R <=0 || C <= 0 || CS <= 0 || W <= 0) exit(0);
	
	if (!(cin >> R >> C >> SC >> CS >> W)){exit(0);}
	if (SC + '0' + CS >= 127) exit(0);
	
	for (int r = 0; r < R; r++){ // loop for rows
		for (int c = 0; c < C; c++){ // loop for column
			for (int w = 0; w < W; w++){ // width of "square" loop
				cout << '0' + (SC + (r+c)%CS);
				}
			}
		
		}
	
	
	
	
	}// End of Main 


The program compiles and runs, but will display no output whatsoever. I realize my W isn't implemented correctly yet (still working on the algorithm), but I feel there should still be some output.
what is the purpose of line 15? Shouldn't that be in place of line 11?
My professor requires we have a check for if cin failed, and if so to exit the program silently.

Would it be better to do this?
while cin>>R >> C >> SC >> CS >> Wl
They way you did it requires you to input it twice. You should just remove line 11 and replace it with line 15.
I did that, now whenever it reads my input it exits the program instantly. Also I still can't figure out how to handle W.
I did that[You should just remove line 11 and replace it with line 15.], now whenever it reads my input it exits the program instantly.

It shouldn't unless you are entering the data in the incorrect order.

Basically if I under stand this correctly if the example with a width of 2 it would look like:

aa bb cc dd aa bb
aa bb cc dd aa bb
bb cc dd aa bb cc
bb cc dd aa bb cc
cc dd aa bb cc dd
cc dd aa bb cc dd
dd aa bb cc dd aa
dd aa bb cc dd aa
aa bb cc dd aa bb
aa bb cc dd aa bb


So lets think about this:

For a grid L x H we would need 2 loops one for rows and one for columns
1
2
for( int row = 0; row < ROWS; ++row )
    for( int column = 0; column < COLUMNS; ++column )


This will go through each tile and each tile is W x W in dimensions so we would need another 2d loop as earlier both will be for the width.

Basically we have the rows for the grid , rows of the element , columns of grid , and columns of element


So it should look like:

1
2
3
4
for( int row = 0; row < ROWS; ++row )
    for( int i = 0; i < WIDTH; ++i )
        for( int column = 0; column < COLUMNS; ++ column )
             for( int j = 0; j < WIDTH; ++j )


So lets look at your formula:
SC + (r+c)%CS


now lets put it into use:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for( int gridrow = 0; gridrow < rows; ++gridrow )
	{
		for( int elementrow = 0; elementrow < width; ++elementrow )
		{
			for( int gridcolumn = 0; gridcolumn < columns; ++gridcolumn )
			{
				for( int elementcolumn = 0; elementcolumn < width; ++elementcolumn )
				{
					cout << static_cast<char>( character + ( gridrow + gridcolumn ) % cycle );
				}
				cout << ' ';
			}
			cout << endl;
		}
	}
aa bb cc dd aa bb 
bb cc dd aa bb cc 
bb cc dd aa bb cc 
cc dd aa bb cc dd 
cc dd aa bb cc dd 
dd aa bb cc dd aa 
dd aa bb cc dd aa 
aa bb cc dd aa bb 
aa bb cc dd aa bb


http://ideone.com/oGmQLj


[edit] For some reason it is not displaying the first
aa bb cc dd aa bb 
in the output of the code I provided I think there is a bug with the "--- aa bb cc dd aa bb " not finding the first line.

Looking at your code you were very close you just had your loop in slight wrong order and you added to the SC for some reason the professor/sample output you had did not.
Last edited on
Topic archived. No new replies allowed.