Displaying Pyramid

Guys I am having trouble coding this one :( Any help please?

Input
You are to ask the user to input from the console(keyboard) two data, M and N. M is an integer (M>0)that tells us the size of the pyramid and N is the character symbol you will use to draw the replica of the pyramid.

Sample Run
Input a number and a symbol: 4 *
           *
  *       * *       *
 * *     * * *     * *
* * *   * * * *   * * *


Here is my partial code. But the problem is when the size of the pyramid is more than 10 there's a bug -_-
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
37
38
39
#include <iostream>
using namespace std;

main()
{
   int a, i, j, h;
   char symbol;

    cout << "Enter the size of the pyramid: ";
    cin >> a;
    cout << "Enter the symbol to be used: ";
    cin >> symbol;
    cout << endl;


    for(i = 1;i <= a;i++){
//1
        for (j = a;j >= i;j--){
        cout << " ";}

        for(h = 1;h <= i-1;h++){
        cout << symbol << " ";}
//2
        for (j = a;j >= i;j--){
        cout << "  ";}

        for(h = 1;h <= i;h++){
        cout << symbol << " ";}
//3
        for (j = a;j >= i;j--){
        cout <<"  ";}

        for(h = 1;h <= i-1;h++){
        cout << symbol << " ";}

    cout << endl;}

system("pause");  
}
Last edited on
i dont think anyone would spend time to make a code example for this but if you come up with one which might have errors or algorithmic mistake or uncompleted. people would like to help you
Last edited on
Like Ceset said, write some code, then ask for help. But just to get you started: it uses loops ;P
Here :)) sorry :)

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <string>
using namespace std;

int main(){
    int i,space,rows,k=0; 
    char inp;
    string tria;
    cout << "number and a symbol: "; 
    cin >> rows >> inp; 
   /* cout << "Enter a character: ";
    cin >> inp;*/
    
    
     for(i=1;i<rows;++i) 
    { 
    for(space=1;space<=rows-i;++space) 
    { 
    cout << " ";
    }
    while(k!=2*i-1) 
    { 
    cout << inp; 
    ++k;
    } 
    k=0; 
    cout << endl; 
    }
     
    for(i=1;i<=rows;++i) 
    { 
    for(space=1;space<=rows-i;++space) 
    { 
    cout << " ";
    }
    while(k!=2*i-1) 
    { 
    cout << inp; 
    ++k;
    } 
    k=0; 
    cout << endl; 
    }
    
     for(i=1;i<rows;++i) 
    { 
    for(space=1;space<=rows-i;++space) 
    { 
    cout << " ";
    }
    while(k!=2*i-1) 
    { 
    cout << inp; 
    ++k;
    } 
    k=0; 
    cout << endl; 
    }
   
    system("pause");
//return 0;
}
actually your code is quite fine just add #include <windows.h> .to use system("pause"); u need to add it. then everything is good to go. if i didnt miss something.
Last edited on
Heres the new one much better but when the rows is more than 10 the pyramids overlap each other.

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
37
38
39
#include <iostream>
using namespace std;

main()
{
   int a, i, j, h;
   char symbol;

    cout << "Enter the size of the pyramid: ";
    cin >> a;
    cout << "Enter the symbol to be used: ";
    cin >> symbol;
    cout << endl;


    for(i = 1;i <= a;i++){
//1st
        for (j = a;j >= i;j--){
        cout << " ";}

        for(h = 1;h <= i-1;h++){
        cout << symbol << " ";}
//2nd
        for (j = a;j >= i;j--){
        cout << "  ";}

        for(h = 1;h <= i;h++){
        cout << symbol << " ";}
//3rd
        for (j = a;j >= i;j--){
        cout <<"  ";}

        for(h = 1;h <= i-1;h++){
        cout << symbol << " ";}

    cout << endl;}

system("pause");  
}
Is pause even needed?

Never mind, though you were running from the cmd line
Last edited on
I'm using Dev C++
well cant say for sure but the reason might be consoles width limit.

i suppose you are working on deitel&deitel c++ book. ignore if i m wrong. yeah it gives you great examples and homework to work on so you can learn it better. but dont spend so much time on one thing. pass to another if you understand the usage of loop
This is one of our take home exams eh? :(
1
2
3
4
5
6
void pause()
{
       cin.ignore(256, '\n');
       cout << "Press ENTER to continue";
       cin.get();
}
well so funny same question over and over hah :D :D these deitel dudes
Last edited on
Topic archived. No new replies allowed.