Click to See Complete Forum and Search --> : Nested Loops: Confusion


chubbs1900
12-09-2007, 08:20 PM
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:
#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..

Viorel
12-10-2007, 04:00 AM
Probably the inner loop should do this:
. . .
for (int numCtrl = 1; numCtrl <= lineCtrl; ++numCtrl) cout << numCtrl;
. . .
I hope this works.