# a slightly different triangle

• 10-11-2003
sweetly
a slightly different triangle
I've looked through many old posts and have not seen one like this. I know creating a triangular shape composed of *'s is common, but this one's a little different.
This is how the output should look:

1
12
123
1234
123
12
1

I have tried to write some code, but I substituted *'s for numbers to see if I could get that to work first. As usual, it's not working. Could anyone give me some idea where I'm going wrong. Thanks.

Code:

``` #include <iostream> using namespace std; //Functions prototypes... void instructions ();        int triangle (); //---------------------------------- int main () {                 instructions ();         triangle (); return 0; } //-------------------------------------------------------------------------- // User instructions void instructions () {         cout << "This program displays the numbers 1 through 4 "                 << "in incremental steps, " << endl;         cout << "starting with just 1 on the first line "             << "and increasing each line by one digit " << endl;         cout << "until the fourth line has all 4 numbers.  "             << "It then starts displaying the numbers" << endl;         cout << "in decremental steps until the final line "                 << "just displays the number 1." << endl << endl; }        // End of instructions function //----------------------------------------------------------------- int triangle () {                 //Local variables:         int horiz=0;         int vert=0;         for(int vert=1; vert<=4; vert++)         {                 for(int horiz=1; horiz<=3; horiz++)                 cout << "*";                 {                         for(int horiz=1;horiz<=vert; horiz++)                                 cout << "*";                 }         cout << endl;         }         return 0; }```
• 10-11-2003
Lynux-Penguin
indeed... thanks for looking at least ^_^

Code:

```int triangle (int n) {         int h,k;         for(h=0;h<n;h++)         {                 for(k=0;k<h;k++)                         cout<<k+1;                 cout<<endl;         }         for(h;h>0;h--)         {                 for(k=0;k<h;k++)                         cout<<k+1;                 cout<<endl;         }         return 0; }```
the parameter n is the outer number of the triangle shape, in your example it was 4.

my first for loop runs forwards counting with an inner loop outputting the numbers counting up to the number of level's its currently on.
then when the function reaches h=n (after the first loop)
I begin the same loop counting backwards back to 0 again.

-I hope that made at least a little sense and can aid you a bit

-LC
• 10-11-2003
confuted
Code:

```int triangle () {         int row,column,iterations;         for(iterations=0;iterations<=10;iterations++)         {                 for(row=1;row<4;row++)                 {                         for(column=1;column<=row;column++)                         {                                 cout<<column;                         }                         cout<<endl;                 }                 for(row=4;row>1;row--)                 {                         for(column=1;column<=row;column++)                         {                                 cout<<column;                         }                         cout<<endl;                 }         }         return 0; }```
Experiment with the numbers and find out what everything does.
• 10-11-2003
sweetly
(trying not to sound like a total idiot)
What are iterations?:D
• 10-11-2003
sweetly
it's almost right....
here's the updated code with the changes.

Code:

```int triangle () {         int row,column,iterations;         for(iterations=1;iterations<=1;iterations++)         {                 for(row=1;row<4;row++)                 {                         for(column=1;column<=row;column++)                         {                                 cout<<column;                         }                         cout<<endl;                 }                 for(row=4;row>1;row--)                 {                         for(column=1;column<=row;column++)                         {                                 cout<<column;                         }                         cout<<endl;                 }         }         return 0; }```
this is the output:

1
12
123
1234
123
12

Now I just need to get the last '1' to print.
• 10-11-2003
confuted
Change this:
for(row=4;row>1;row--)
to this:
for(row=4;row>0;row--)
or this:
for(row=4;row>=1;row--)

then. "Iterations" means "repetitions." I wrote the code the way I did so that it wouldn't look like this:
Code:

```1 12 123 1234 123 12 1  //this 1  //this 12 123 1234 123 12 1```
Anyway, man, I told you to mess with those numbers to figure out what they did. Why didn't you?
• 10-12-2003
Lynux-Penguin
lol mine worked, and I kind of had an explanation.

-LC
• 10-13-2003
sweetly
I really do appreciate your help (both of you).

Confuted:
I did mess around with the numbers....and I got pretty close, all I was missing was the '=' . Nonetheless, I do appreciate your patience with a noobie.

Lynux-Penguin:
I did try to use your code, but I couldn't get it to work. I kept getting an error or 2. I probably didn't set it up right.