Symmetrical Number Pyramid Help Please

This is a discussion on Symmetrical Number Pyramid Help Please within the C++ Programming forums, part of the General Programming Boards category; Write a C++ program which will ask the user to enter an integer from 1 to 15 and displays a ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    3

    Symmetrical Number Pyramid Help Please

    Write a C++ program which will ask the user to enter an integer from 1 to 15 and displays a number pyramid (implement input validation). For example, if the input is 4 the output will be as follows:
    * * *1
    * * 2 1 2
    * 3 2 1 2 3
    4 3 2 1 2 3 4
    My professor said we must use nested loops.
    This is what i have so far the 2nd inner loop cause a half pyramid. The 1st inner loop I am trying to make the opposite of the 2nd inner loop. I am unsure you can logically do that however.

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    
    int main ()
    {
        int n , i , j , k;
        do 
        {
            cout << "Enter an integer between:" << endl;
                cin >> n;
            cout << endl;
        }
        while ( n < 0 || n > 16 );
        
        for ( i = 0 ; i < n ; i++ )
        {
            for( j = 1 ; j = 1 ; i++)
            {
                cout << right << setw(3) << j;
            }
                for ( k = 1 ; k <= i + 1 ; k++ )
                {
                    cout << left << setw(3) << k;
                }
            
            cout << endl;
        }
        
    return 0;
    }
    Last edited by HunterTnTU; 10-11-2011 at 05:03 PM. Reason: Cannot figure out how to get the code tagged properly

  2. #2
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    I would suggest working it out in steps.

    1. figure out how you would print "* * * 1" if 4 is entered.
    2. Next figure out how you would print the next line, " * * 2 1 2".

    After that you should be able to use the same algorithm to print the rest.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    3
    I forgot to say that the "*"s are not part of the out put they are just to keep the spaces for some reason the spaces went away and this happened:
    1
    2 1 2
    3 2 1 2 3
    4 3 2 1 2 3 4
    Sorry for that confusion I should of clarified that first.

  4. #4
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    No problem, you would go about it the same way. Also, I noticed your while loop is not doing what you think it's doing, you need brackets.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    3

    Update * Close but no cigar*

    So, I have tweaked it some and now I have it giving the out put
    1
    212
    23123
    2341234

    I am having a major pain trying to figure out how to make the 2nd inner loop reverse the number order and make the out put take on the form of a triangle. Maybe its because of frustration.

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    
    int main ()
    {
        int n , i , j , k;
        
            cout << "Enter an integer between 1 and 15:" << endl;
                cin >> n;
            cout << endl;
        
        while ( n < 0 || n > 16 )
        {
            cout << "Invalid input! Input must be between";
        }
        
        for ( i = 0 ; i < n ; i++ )
        {
            for( j = 2 ; j <= i + 1 ; j++)
            {
                cout << setw(3) << j;
            }
                for ( k = 1 ; k <= i + 1 ; k++ )
                {
                    cout << setw(3) << k;
                }
            
            cout << endl;
        }
        
    return 0;
    }

  6. #6
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Instead of counting up your j for loop, count down. It should print

    1
    21
    321
    4321

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Star Pyramid
    By subhadeepgayen in forum C Programming
    Replies: 2
    Last Post: 08-10-2010, 06:16 AM
  2. [ask]pyramid number with loop
    By nitediver in forum C Programming
    Replies: 2
    Last Post: 10-13-2009, 08:57 AM
  3. Number pyramid
    By Tehy in forum C Programming
    Replies: 7
    Last Post: 05-31-2007, 10:01 AM
  4. Making a pyramid of Xs
    By Tride in forum C++ Programming
    Replies: 8
    Last Post: 12-12-2003, 05:14 PM
  5. stupid pyramid
    By nikki19 in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2002, 11:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21