Making a Diamond

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

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>
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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
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.
1
2
3
4
5
6
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).
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
#include <iostream>

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.