### loopy fun :)

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)
Last edited on
Two nested for loops.

 ``12345678910111213`` ``````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.
You could. It's much less intuitive to do it in one loop, though.
It's a two-step problem. You count up one way, count down the other.

 ``1234567891011121314151617181920`` ``````bool countingUp = true; int i = 2; while(i > 0) { for(int j = 0; j < i; ++j) cout << "#"; cout << endl; if(i == 8) { for(int j = 0; j < i; ++j) cout << "#"; cout << endl; } if(i >= 8) countingUp = false; if(countingUp) i+=2; else i-=2; }`````` ```## #### ###### ######## ######## ###### #### ##```

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.
Found a nice solution :) Take a look:

 ``12345678910`` ``````for(int i = 8; i >= -8; i -= 2) { if(i == 0) continue; for(int r = 12 - abs(abs(i) - 10) / 2; r ; r--) cout << " "; for(int r = abs(abs(i) - 10); r ; r--) cout << "#"; cout << "\n"; }``````

If I have to break one of my "rules" I would rather just add an if statement :)

Made a really cool shape by mistake, it looks like a K with a logo style look.

 ``12345678910111213141516`` ``````int x = 12; for(int i = x, sp = 12; i >= -4; i--, sp -= 4) { if(i == 0) continue; for(int r = abs(abs(i) - x); r ; r--) cout << " "; for(int r = abs(abs(i) - x); r ; r--) cout << "#"; for(int r = abs(abs(sp) - 12); r ; r--) cout << " "; for(int r = abs(abs(i) - x); r ; r--) cout << "#"; cout << "\n"; }``````
Last edited on
Topic archived. No new replies allowed.