Thread: a slightly different triangle

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    48

    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;
    }

  2. #2
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    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
    Last edited by Lynux-Penguin; 10-11-2003 at 06:25 PM.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  3. #3
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    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.
    Away.

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    48

    Talking

    (trying not to sound like a total idiot)
    What are iterations?

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    48
    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.

  6. #6
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    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?
    Away.

  7. #7
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    lol mine worked, and I kind of had an explanation.

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    48
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive Triangle Function
    By w2look in forum C Programming
    Replies: 14
    Last Post: 11-13-2010, 02:31 PM
  2. Right Triangle Program
    By BSmith4740 in forum C# Programming
    Replies: 9
    Last Post: 02-27-2008, 12:24 AM
  3. Stupid Logic Problem Need Outside Viewpoint
    By RP319 in forum C++ Programming
    Replies: 5
    Last Post: 03-03-2005, 10:59 PM
  4. Just in case: "Odd" Triangle Challenge (for me)
    By BB18 in forum C Programming
    Replies: 3
    Last Post: 10-09-2004, 12:02 AM
  5. Determining a Triangle using get and pointer
    By naynay in forum C Programming
    Replies: 7
    Last Post: 04-11-2003, 05:55 AM