Drawing Pyramids

I want to draw pyramids near each other in the same lines but I only know how to draw it under each other. Here is my Code

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
#include <iostream>
using namespace std;
int main()
{
    int D , N=0;
    int sum=0;
    int i,space,k=0;
    cin >> N >> D;

    int rows[N];


    for (int j=0;j < N;j++){
        cin>>rows[j];
    }
    for (int j=0;j < N;j++){
        sum = sum + (rows[j]*rows[j]);
    }
      cout << sum << endl;
    if (D == 1)
    {
    for (int j=0;j < N;j++){

    for(i=1;i<=rows[j];++i)
    {
        for(space=1;space<=rows[j]-i;++space)
        {
           cout<<"  ";
        }
        while(k!=2*i-1)
        {
           cout<<"* ";
           ++k;
        }
        k=0;
        cout<<"\n";
    }
    }
    }
    return 0;
}


Output is Like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Inputs
3 1  // Here 2 = Pyramids Number and 1 = Draw
3 2 3 // Number Rows of 1st , 2nd and 3rd Pyramids
// Outputs
22 // Numbers of stars
// The problem here ↓↓↓

    *
  * * *
* * * * *
  *
* * *
    *
  * * *
* * * * *


It should be like this
1
2
3
4
5
6
7
8
9
// Input
3 1  
3 2 3
// Output here
22

  *      *  
 ***  * *** 
*************

There are multiple ways to approach this, some more elegant than others.

To tackle this problem in a more simple/straightforward way, I would view each drawn pyramid as a variable (user-defined) number of rows with the same number of columns. This makes a block to print to the screen. Ex: (# stands for blank space)
1
2
3
##*##
#***#
*****


Using this, we know that if we store these blocks by their row lines, we can print them out all at once. Ex.

1
2
3
  *   *
 *** ***
*****


is made out of
1
2
3
  *
 ***
*****


and

1
2
 *
***


However, rather than print one block and then the other, we print the first line of each, and then the next until each pyramid is finished. If one pyramid is smaller than the other, you just continue printing lines of spaces at the column length of that pyramid.

If you want your pyramids on level ground together, you would print empty space on top of the smaller pyramids first.

(This actually feels like a really fun problem!)
Sorry Can you explain more because I am newbie in C++ ? and can you build the code ?
Sorry Can you explain more because I am newbie in C++ ?

This isn't as much a C++ question as it is an abstract question. Implementing in C++ is part of it, but thinking about how you would do this is the real challenge here.

and can you build the code ?

If you are asking whether or not I can build your code:
Yes, and it ran just as you said it would.

If you are asking me to write code for you:
No, I am not supposed to do your homework for you. Sitting down and solving a problem like this is one of the best things about programming. Have fun with it and learn by trying different things until you get it right :)

Regarding one method I suggested earlier, here is some psuedo-code of what I think it would look like (I could be wrong (or inefficient) as I have not implemented it yet!)

-get number of pyramids
-get sizes of pyramids
-get max number of rows

-for each row
-----for each pyramid
----------print out the line for that row
Last edited on
Can you explain more ? exactly here ----------print out the line for that row
closed account (48bpfSEw)
sometimes it's easer to invert the approach. draw first the base of these pyramids then the next layer then the next layer etc.

"draw" means in this case : store in a string-array.

at the end: print the result invers out.
Last edited on
I made this code , But the problem here is that the code is drawing the same pyramids near each other instead of each pyramids

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
#include <iostream>
using namespace std;
int main()
{
    int D=0 , N=0;
    int sum=0;
    int i,space,k=0;
    cin >> N >> D;

    int rows[N];


    for (int j=0;j < N;j++){
        cin>>rows[j];
    }
    for (int j=0;j < N;j++){
        sum = sum + (rows[j]*rows[j]);
    }
      cout << sum << endl;
    if (D == 1)
    {
    for (int j=0;j < N;j++){

    for(i=1;i<=rows[j];++i)
    {
        for (int I=0;I < N;I++){
        for(space=1;space<=rows[j]-i;++space)
        {
           cout<<"#";
        }
        while(k!=2*i-1)
        {
           cout<<"*";
          k++;
        }
        k=0;

       }
        cout<<"\n";
    }
    }
    return 0;
}
}


Output
1
2
3
4
5
6
7
8
9
##*##*##*
#***#***#***
***************
#*#*#*
*********
##*##*##*
#***#***#***
***************
// Same Example in the Main question  


EDITED : I use # instead of " " to make it clear
Last edited on
Topic archived. No new replies allowed.