For loop * checkerboard

I have to make a program for my class that needs me to use 3 cout statements that are "*" ," ", and endl. I need to make it an 8 by 8 checkerboard of *s and it has to be a for loop. Kind of lost any help?

#include <iostream>
using namespace std;

int main ()

{

for (int i = 0; i < 8; i ++)
{
for (int a = 1; a <=16; a ++)
{
if ((a % 2 ))
}
}



}
That's what I've come up with so far.
Some clues ....
1
2
3
4
5
6
7
8
9
10
11
int count = 0;

for (int Row = 0; Row < 8; Row++) {
       for (int Col = 0; Col < 8; Col++) {
             //some stuff here
                   std::cout << "*";
             //some stuff here
              std::cout << std::endl;
              count++;
       }
}


If you want to print 8 by 8, you need 8 as the limit in the loops. Always start your loops at zero.

Use meaningful names, not i & j as variable names.

HTH

Last edited on
@Brandonyoungblood


How does the checkerboard look? Does it look as in the picture below?

1
2
3
4
5
6
7
8
* * * *
 * * * *
* * * *
 * * * *
* * * *
 * * * *
* * * *
 * * * *



@TheIdeasMan
Use meaningful names, not i & j as variable names.


i and i are meaningful names for professinal programmers and have their roots in the FORTRAN.
Last edited on
throw in a " " in a extra cout in your inner loop and a endl in the outter loop and "Bam" you have a
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
@vlad from moscow
i and i are meaningful names for professinal programmers and have their roots in the FORTRAN


Well that may be so, but I have seen errors happen time & time again, in this situation. That kind of error is particularly hard to see & the compiler doesn't warn you. On could deduce the problem in the debugger, but I would rather it didn't happen at all, or detect it much earlier. The problems were solved by doing a find replace on the i & j variables, changing them to Row & Col - the error becomes apparent.

The main cause of problems is that i & j look so similar, and they are near to each other on the keyboard. Single char names might be OK for loops, but one could at least could choose letters that look different - like r & c for example. I still prefer a very short or abbreviated word with meaning instead.

You have already made a mistake in your reply -
i and i are
- you were typing fast weren't you? That shows how easy it is to create a problem.

Now I do respect your knowledge & ability (much much more than mine), but I don't understand why people object to such a simple & possibly error preventing convention.

I guess programming is like maths in a lot of ways, so there are situations where a single char name might be fine. We were involved in a topic quite a while back about factorial(n). Obviously we have x, y, z for coordinates, but should one use Radius or r ; or key & value instead of k & v ; or deg, min & sec instead of d,m,s.

The thing I don't like is seeing a whole page of code with single char variable names, sure we should have comments for variable names at declaration, but I can't help thinking of the weary maintenance programmer who has to understand it all.

Of course one can argue it is personal preference, but I am promoting a convention that might help prevent errors.

I have been involved in a big long discussion about this in the past, hopefully this won't turn into another one.
@vlad from moscow yes.

Also I don't really see why there is a concern for variable names, it's what I learned and prefer for the simplicity of the programs we're doing in my class. When I make programs with much more detail and stuff going own I use full names to clarify for myself and others what is what. Also I and J? Never used both I used I and A.
@theideasman
I agree let's not debate over this not a big deal.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main()
{
	const size_t N = 8;
	const char c1 = '*', c2 = ' ';

	for ( size_t i = 0; i < N; i++ )
	{
		for ( size_t j = 0; j < N; j++ )
		{
			std::cout << ( ( i + j ) % 2 == 0 ? c1 : c2 );
		}
		std::cout << std::endl;
	}

	return 0;
}
Topic archived. No new replies allowed.