Thread: Nested Loops: Confusion

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    4

    Nested Loops: Confusion

    Hey guys, I am trying to create a number triangle that looks like this.

    123456789
    12345678
    1234567
    123456
    12345
    1234
    123
    12
    1

    This is where I'm at:
    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
        int limit;
    
        // Read limit
        cout << "Please enter a number between 1 and 9: ";
        cin  >> limit;
    
        for (int lineCtrl = limit; lineCtrl >= 1; lineCtrl--)
            {
             for (int numCtrl = lineCtrl;
                      numCtrl >= 1;
                      numCtrl--)
                cout << numCtrl;
             cout << endl;
           } // for lineCtrl
        system ("pause");
        return 0;
    }
    And i get this as a result:

    987654321
    87654321
    7654321
    654321
    54321
    4321
    321
    21
    1

    I am told that I don't even need to decrement the inner loop and just have it return 1 to lineCtrl but I have no idea how to do that. The book I'm reading is only giving me examples how to write nested loops in blocks..

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Trace through the program manually and write down on paper exactly what the program is doing at what stage. You should be able to figure out how to change the inner loop after that.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    4
    haah your right, I just had to reverse the outer loop and keep the inner loop positive!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Evaluation of nested loops
    By Mister C in forum C Programming
    Replies: 2
    Last Post: 08-13-2004, 01:47 PM
  2. nested for loops
    By akub3 in forum C Programming
    Replies: 2
    Last Post: 04-01-2004, 06:21 AM
  3. nested for loops and bank account interest help!!
    By webvigator2k in forum C++ Programming
    Replies: 1
    Last Post: 04-07-2003, 08:03 PM
  4. Output from nested loops
    By IzaakF in forum C Programming
    Replies: 2
    Last Post: 09-01-2002, 06:09 AM
  5. nested for loops
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 11-17-2001, 11:44 AM