Thread: Help making Triangle

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    6

    Help making Triangle

    Hi again all,

    I am having some troubles making this program. Here is the assignment:

    Code:
    Write a computer program that prints a triangle of digits. Your program must read two positive integers as follows: 
    the width of the triangle, and
    the position of the triangle relative to the left margin (the amount to indent the triangle, in other words).
    You can assume that the user will always enter non-zero positive integers. 
    
    The triangle is drawn upside-down using single digit integers. For example, a width 3 indent 3 triangle looks like this: 
       123 
       12 
       1 
    
    ... and a width 13 triangle indent 0 triangle looks like this: 
    1234567890123 
    123456789012 
    12345678901 
    1234567890 
    123456789 
    12345678 
    1234567 
    123456 
    12345 
    1234 
    123 
    12 
    1
    and here is my code:
    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
    	int width = 0;
    	int indent = 0;
    	int digit = 1;
    
    	cout<<"Enter the width of the triangle: ";
    	cin>>width;
    	cout<<"Enter the position relative to the left margin (the indent): ";
    	cin>>indent;
    
    	for (int x = 0; x < width; x++)
    	{
    		for (int i = 0; i < indent; i++)
    		{
    			cout<<" ";
    		}
    		for (int w = 0; w < width; w++)
    		{
    			cout<<digit%10;
    			digit++;
    		}
    		width--;
    		digit = 1;
    		cout<<endl;
    	}
    	return 0;
    }
    When the program is run it looks like this:
    Code:
    Enter the width of the triangle: 5
    Enter the position relative to the left margin (the indent): 3
       12345
       1234
       123
    I understand why it quits short but am unable to figure out a fix for the problem.

    Anyone got some suggestions? Thank you!
    Last edited by jensklemp; 02-11-2010 at 03:46 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Make your outer loop count backwards.

    > int main = 0;
    And width is undeclared - are you sure this is REALLY your code?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    6
    Sorry somehow my brain told me to write main instead of width which I wanted.
    Why would I want to count backwards?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Sorry somehow my brain told me to write main instead of width which I wanted.
    How about just copy/paste from your actual source code?

    It saves you time, and we don't waste more time pointing out trivial mistakes only for you to say "oh, that's not in the real code".

    > Why would I want to count backwards?
    Because the lines get shorter (perhaps)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    6
    YAY! I got it, sorry to waste your time. I created a new variable and called it length, this way it didn't affect the outer loop. Now it looks like this:

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
    	int width = 0;
    	int indent = 0;
    	int digit = 1;
            int length = 0;
    
    	cout<<"Enter the width of the triangle: ";
    	cin>>width;
    	cout<<"Enter the position relative to the left margin (the indent): ";
    	cin>>indent;
    
            length = width;
    
    	for (int x = 0; x < width; x++)
    	{
    		for (int i = 0; i < indent; i++)
    		{
    			cout<<" ";
    		}
    		for (int l = 0; l < length; l++)
    		{
    			cout<<digit%10;
    			digit++;
    		}
    		length--;
    		digit = 1;
    		cout<<endl;
    	}
    	return 0;
    }

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. Clipping a Triangle of Digits
    By towely in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2009, 07:00 AM
  3. help making triangle
    By dbzx in forum C Programming
    Replies: 3
    Last Post: 04-05-2009, 01:09 AM
  4. Right Triangle Program
    By BSmith4740 in forum C# Programming
    Replies: 9
    Last Post: 02-27-2008, 12:24 AM
  5. Replies: 14
    Last Post: 11-09-2005, 11:28 PM