I had some fun with loops creating different shapes with hashes(#) and spaces( ). I was wondering if anyone knew a way to make a loop go through these numbers:
2 4 6 8 8 6 4 2
This will enable me to make a shape like this:
##
####
######
########
########
######
####
##
NOTE: I was able to solve this with 1 line of '#' in the middle that is bigger than the others using the numbers
2 4 6 8 10 8 6 4 2
but I can't think out a formula that allows me to have two 8s in the middle. (The book that I got the problem from doesn't include solutions and there is no solutions online either)
for(int i = 2; i <= 8; i+=2) //increment i by 2 until i reaches 8
{
for(int j = 0; j < i; ++j) //for each step, output i number of '#'
cout << "#";
cout << endl;
}
for(int i = 8; i >= 2; i-=2) //doing the same thing backwards.
{
for(int j = 0; j < i; ++j)
cout << "#";
cout << endl;
}
##
####
######
########
########
######
####
##
So that gives you the basic idea. All that's left is to add the right amount of spaces to each line (which can be done by modifying the above for loops).
Is there a way to combine the two loops together to make one loop? That is what I am trying to do. I was able to create one, but I couldn't find a way to get two eights in the middle of the loop without an if statement or extra loop. Thanks for your help though, it may not even be possible to do it in one loop but I thought maybe someone in the forums may know a way. Worth a try.
So yeah. It's way more intuitive to do it with two for loops, one for each counting direction. The above would probably be frowned on because it's unnecessarily complicated.