Hey guys, I am trying to self teach C++ and I am to the point of loops. Now I was working on how to create a triangle with manipulating nested loops to create a triangle with numbers. Here is the code I have ended up with:
/* Print a number series from 1 to a user-specified
limit in the form of a right triangle.
Written by: Chubbs1900
Date: 12/02/2007
*/
#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 = 1; lineCtrl <= limit; lineCtrl++)
{
for (int numCtrl = 1;
numCtrl <= lineCtrl;
numCtrl++)
cout << numCtrl;
cout << endl;
} // for lineCtrl
return 0;
} // main
/* Results
Please enter a number between 1 and 9: 5
1
12
123
1234
12345
123456
*/
What I would like to do is manipulate this code to get this:
123456789
-23456789
--3456789
---456789
----56789
-----6789
------789
-------89
--------9
(Minus the dashes)
Now i was able to reverse the triangle with
for (int lineCtrl = limit; lineCtrl >= 1; lineCtrl--)
{
}
on the outer loop... But then i have to modify the inner loop to instead of starting from 1 each time, start from a value that increments the start value. So the first time it may start with one, but the second time the inner loop is reached, it will start from 2 and go to 9, next pass it will start at 3 and go to 9 until finally it reaches 9.
Lastly, the spacing.... crap....
HELP