Need to combine two programs to make one shape/program

I have two parts to a diamond generator that I need to combine to make one program. the stipulations are that a user must enter the number of rows and the diamond must be made out of user inputted symbol such as $ or #.

code for top half:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;
int main()
{
    int i,space,rows,k=0;
    cout<<"Enter the number of rows: ";
    cin>>rows;
    for(i=1;i<=rows;++i)
    {
        for(space=1;space<=rows-i;++space)
        {
           cout<<"  ";
        }
        while(k!=2*i-1)
        {
           cout<<"* ";
           ++k;
        }
        k=0;
        cout<<"\n";
    }
    return 0;
}


code for bottem half

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
int main()
{
    int rows,i,j,space;
    cout<<"Enter number of rows: ";
    cin>>rows;
    for(i=rows;i>=1;--i)
    {
        for(space=0;space<rows-i;++space)
           cout<<"  ";
        for(j=i;j<=2*i-1;++j)
           cout<<"* ";
        for(j=0;j<i-1;++j)
           cout<<"* ";
        cout<<endl;
    }
    return 0;
}
You just had to copy the bottom part and paste it at the end of you first part and delete already declared variables.

Here, I did it for you,
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;
int main()
{
    int i,space,rows,k=0;
    cout<<"Enter the number of rows: ";
    cin>>rows;
    for(i=1;i<=rows;++i)
    {
        for(space=1;space<=rows-i;++space)
        {
           cout<<"  ";
        }
        while(k!=2*i-1)
        {
           cout<<"* ";
           ++k;
        }
        k=0;
        cout<<"\n";
    }
    
    int j;
    for(i=rows;i>=1;--i)
    {
        for(space=0;space<rows-i;++space)
           cout<<"  ";
        for(j=i;j<=2*i-1;++j)
           cout<<"* ";
        for(j=0;j<i-1;++j)
           cout<<"* ";
        cout<<endl;
    }
    
    return 0;
}



Hope this helps!
Last edited on
that did help but there is some more issues i need to be able to ask the user what he wants the shape to be made of. the other issue is that if i enter 5 it comes out to 10 rows
You do already ask the user for the number of rows, so it should not be difficult to ask for a symbol; a character.

Your current output writes hardcoded two-character strings "* ". You would output the chosen symbol from variable and then a whitespace.


Number of rows is math. The user gives a total. Each part must do about half of total. "About", because odd and even total are perhaps a tiny bit different?
Topic archived. No new replies allowed.