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

>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.
Currently you set numCtrl to 1. What happens when you set it to 1 + linCtrl? :)

>Lastly, the spacing.... crap....
Add another loop next to the numCtrl loop that prints the correct number of spaces.

It returns 9 and 9 spaces below it before the return statement

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+ lineCtrl;
                  numCtrl <= lineCtrl;
                  numCtrl++)
            cout << numCtrl;
         cout << endl;
       } // for lineCtrl
       system ("pause");
    return 0;

About the spacing: You mean write another nested for loop?

>It returns 9 and 9 spaces below it before the return statement
Good. And what happens when you change numCtrl <= lineCtrl to numCtrl <= limit ? I'm basically forcing you to do the experimentation that you should be doing with these questions, by the way.

>About the spacing: You mean write another nested for loop?
Yep, you can do that:

for ( int i = 0; i < 10; i++ ) {
  for ( int j = 0; j < i; j++ )
    cout<<' ';

  for ( int k = 0; k < i; k++ )
    cout<<'*';

  cout<<'\n';
}

I have done the spacing with an else statement but I couldn't get it to go before the number output to create the spacing i needed. I have had it done for me in other forums but i don't understand how.

for (int col = 1; col <= limit; col++)
	         if (row >= col)
	             cout << col;
	         else
	             cout << ' ';

This is what experimented with in a past tutorial. I am really bad at reverse engineering and end up having to re-write.. as i did with this... (dyslexia)

My problem is, is that I am really not grasping the loop concept because the book i have reads the chapter in loops bigger than any other chapter int he book.. Then i go over to tutorials and find that they only cover the topic in 2 pages... im going crazy

I need to run to work, but Im gonna print out everything over there and figure this out.... Thank you so much for the help... All the testing i was doing was changing over the more than or greater than symbols and trading out variables.. this will help.. I will be back

What if I wanted to do this???

123456789
12345678
1234567
123456
12345
1234
123
12
1

I am trying to simplify by taking out the spacing

Yeah, it's functional now~!

/*	
    Print a number series from 1 to a user-specified  
	limit in the form of a inverted right triangle.
	   Written by: Chubbs1900
	   Date: 12-09-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 = limit; lineCtrl >= 1; lineCtrl--)
        {
         for (int numCtrl = 1;
                  numCtrl <= lineCtrl;
                  numCtrl++)
            cout << numCtrl;
         cout << endl;
       } // for lineCtrl
    system("pause");
    return 0;
}    // main 

/* Results

Please enter a number between 1 and 9: 5
12345
1234
123
12
1
*/

what if i want to do this:
* * * * *
* * * *
* * *
* *
*

what would be the codes then?
:D
the shape should be like an INVERTED TRIANGLE, starting at the base of a triangle then to the midpt of it. w/ spaces in between and a space before the frst * @ d next line.

-the image is not actually wt i wanted to be seen. hope u gt my point.

chubbs1900>I am trying to self teach C++ and I am to the point of loops. ...

adatapost>Tryout.

1. 
1 1 1 1 1
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
1 1 1 1 1

2.
1 0 0 0 1
0 1 0 1 0
0 0 1 0 0
0 1 0 1 0
1 0 0 0 1

3.
1
2  4  3
5  7  9  10  8 6
11  13  15  17  18  16 14  12
Comments
Sorry, but you're a year too late!! BTW, very useless post!
This article has been dead for over six months. Start a new discussion instead.