### Making a Diamond

So I have to write a program that makes the shape of a diamond. The diamond looks like this:

 ``123456789101112131415161718192021222324252627282930313233343536`` ``````#include using namespace std; void diamond_line(int spaces, int pluses) { for(int s = 0; s < spaces; s++) cout << " "; for(int p = 0; p < pluses; p++) cout << "+"; cout << endl; } int main() { diamond_line(9, 2); diamond_line(8, 4); diamond_line(7, 6); diamond_line(6, 8); diamond_line(5, 10); diamond_line(4, 12); diamond_line(3, 14); diamond_line(2, 16); diamond_line(1, 18); diamond_line(0, 20); diamond_line(0, 20); diamond_line(1, 18); diamond_line(2, 16); diamond_line(3, 14); diamond_line(4, 12); diamond_line(5, 10); diamond_line(6, 8); diamond_line(7, 6); diamond_line(8, 4); diamond_line(9, 2); return 0; }``````

I know there is a simplier way to do this using nested loops but I just can't figure this one out. Can someone help me?
> using nested loops

You don't need nested loops, do you?
 ``123456789101112131415161718`` ``````#include using namespace std; void diamond_line(int spaces, int pluses) { for(int s = 0; s < spaces; s++) cout << /*" "*/ ' ' ; for(int p = 0; p < pluses; p++) cout << /* "+" */ '+' ; cout << /*endl*/ '\n' ; } int main() { for( int line = 1 ; line <= 10 ; ++line ) diamond_line( 10-line, line*2 ) ; for( int line = 11 ; line < 20 ; ++line ) diamond_line( line-10, (20-line)*2 ) ; }``````
Well it comes out of the nested loops chapter in my book...?
And also that program that you posted leaves out a line of +'s. The center line of 20 +'s, there are 2 of those lines, not 1.
> And also that program that you posted leaves out a line of +'s.
> The center line of 20 +'s, there are 2 of those lines, not 1.

Mea culpa.
 ``123456`` ``````int main() // *** corrected { for( int line = 1 ; line <= 10 ; ++line ) diamond_line( 10-line, line*2 ) ; for( int line = /*11*/ 10 ; line < 20 ; ++line ) diamond_line( line-10, (20-line)*2 ) ; }``````

> Well it comes out of the nested loops chapter in my book...?

Without the function, we have nested loops. (Writing the function is better).
 ``12345678910111213141516171819202122232425`` ``````#include int main() { const char SPACE = ' ' ; const char PLUS = '+' ; for( int line = 1 ; line <= 10 ; ++line ) { const int spaces = 10-line ; for( int s = 0 ; s < spaces ; ++s ) std::cout << SPACE ; const int pluses = line*2 ; for( int p = 0 ; p < pluses ; ++p ) std::cout << PLUS ; std::cout << '\n' ; } for( int line = 10 ; line <= 20 ; ++line ) { const int spaces = line-10 ; for( int s = 0 ; s < spaces ; ++s ) std::cout << SPACE ; const int pluses = (20-line)*2 ; for( int p = 0 ; p < pluses ; ++p ) std::cout << PLUS ; std::cout << '\n' ; } }``````
Ok the one you just posted has everything that I'm familiar with. Thank you so much!
Topic archived. No new replies allowed.