Click here to Skip to main content
15,915,093 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
______4
____4 3 4
__4 3 2 3 4
4 3 2 1 2 3 4
__4 3 2 3 4
___ 4 3 4
______4

Write a C++ program to obtain the above double pyramid.
'_' - this indicates space, that's how it is a pyramid.

What I have tried:

#include <iostream>

using namespace std;

int main()
{
     int i,j;
     for(i=4; i>=1; i--){
        for(j=1; j<=i; j++){
           cout<<" ";
        }
     for(int j=4; j>=i; j--){
        cout<<j;
     }
          for( j=i+1; j<=4; j++){
            cout<<j;
            cout<<'\n';
          }
          for(i=2; i<=4; i++){
            for(j=1; j<i; j++){
                cout<<j;
            }
            for(j=1+i; j<=4; j++){
                cout<<j;
                cout<<'\n';
            }
          }

     }
      return 0;
}


I have tried this and I have a feeling that I have done some really stupid mistake. Please help me.
Posted
Updated 7-Sep-18 9:14am

Lets guess the output have too many lines
C++
for( j=i+1; j<=4; j++){
    cout<<j;
    cout<<'\n'; // Remove this line
}
cout<<'\n'; // and put it there

Your program is complicated and for size 4 only.
a little analyze should help to get it better. The diamond have a vertical and horizontal symmetry, all is around the center (1)
C++
Size Rows Cols Center
 1    1    1    0,0
 2    3    3    1,1
 3    5    5    2,2
 4    7    7    3,3

Should not be complicated to store the Size in a variable and reduce Rows, Cols and Center.
You can notice that all '2' are at a distance of 1 col or 1 row from center.
'3' are 1 step further: distance in rows + distance in cols give you the value at position.
if value is more the the size, print a space.
I let you fill the holes as an exercise.

Advice:
- Learn one or more analyze methods, E.W. Djikstra/N. Wirth Stepwize Refinment/top-Down method is a good start.
Structured Programming.pdf[^]
https://en.wikipedia.org/wiki/Top-down_and_bottom-up_design[^]
https://en.wikipedia.org/wiki/Structured_programming[^]
https://en.wikipedia.org/wiki/Edsger_W._Dijkstra[^]
https://www.cs.utexas.edu/users/EWD/ewd03xx/EWD316.PDF[^]
 
Share this answer
 
Comments
Usman Hunjra 11-Sep-18 17:47pm    
+5 ;)
Patrice T 11-Sep-18 17:54pm    
"+5" mean that you rated the solution 5 stars, which you did not. :)
Usman Hunjra 29-Jan-19 12:48pm    
I just did that Sir .. ;)
Start by indenting your code properly, or, if you are going to use the execrable 1TB then at least consistently:
int main(){
    int i,j;
    for(i=4; i>=1; i--){
        for(j=1; j<=i; j++){
            cout<<" ";
            }
        for(int j=4; j>=i; j--){
            cout<<j;
            }
        for( j=i+1; j<=4; j++){
            cout<<j;
            cout<<'\n';
            }
        for(i=2; i<=4; i++){
            for(j=1; j<i; j++){
                cout<<j;
                }
            for(j=1+i; j<=4; j++){
                cout<<j;
                cout<<'\n';
                }
            }
        
        }
    return 0;
    }
And now you can see what you are doing. And the first thing that springs to mind is this: you have two loops, both altering i and one of them is inside the other ... so your code never exits.
 
Share this answer
 
Comments
jeron1 7-Sep-18 10:44am    
OG wrote: "1TB"

I never know what that format was called, I just cringe when I see it. Apparently the format I favor is called Allman (I never knew that either), I attribute that to the fact that I'm a big Allman Bros. fan! LOL
OriginalGriff 7-Sep-18 11:31am    
I agree - difficult to read, that one.
I prefer Whitesmiths:
if (a)
   {
   b = c;
   }
jeron1 7-Sep-18 11:49am    
Not bad, I never realized how many formats have been categorized.
Rick York 7-Sep-18 13:48pm    
Sometime you should have a look at the UniversalIndentGUI. It's a front end for some code "beautifiers." It has about ten different styles of indentation and it's fairly customizable. It doesn't fully handle how I like to do things but it's close.
jeron1 7-Sep-18 15:46pm    
Interesting, thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900