Results 1 to 10 of 10

Thread: Triangles

  1. #1
    CodeDog is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default Triangles

    Been working on this awhile and any help would be appreciated..Need to make 3 triangles using Scanner class(This part isn't part of my code yet, been concentrating on getting triangles right)
    Triangle shapes are:

    1
    22
    333
    4444
    55555

    1
    222
    33333
    4444444
    555555555

    555555555
    4444444
    33333
    222
    1

    Here's what I have so far. The last 2 Triangles are actually pascal triangles, why it's coming out in the thread like it is, I don't know...

    Java Code:
       class TriangleTest1
       {
           public static void main(String[] args)
           {
               int counter = 1;
               int toPrintT1 = 1;
               int toPrintT2 = 1;
               int toPrintT2A = 1;
    
    
               while (toPrintT1 < 6)
               {
                   for ( int i = 0; i < toPrintT1; i++)
                   {
                       System.out.print(toPrintT1);
    
                   }
                   
                   System.out.println();
                   toPrintT1++;
               }
               System.out.println();       
    
    
               while (toPrintT2 < 6)
               {
                   for (int j = 0; j < toPrintT2; j++)
               {
                       System.out.print(toPrintT2);
                   }
    
                       if ( toPrintT2!= 1)
                       {
                           for (int k = 0; k < toPrintT2A - 1; k++)
                           {
                               System.out.print(toPrintT2A);
                           }
                       }
                       System.out.println();
                       toPrintT2A++;
                       toPrintT2++;
                }
    
            }
       }
    Last edited by CodeDog; 10-12-2008 at 11:12 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    I missed your question. Can you show what the program outputs now and what you want it to be?

  3. #3
    CodeDog is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default

    Output is:

    1
    22
    333
    4444
    55555

    1
    22 2
    333 33
    4444 444
    55555 5555

    First one is correct...
    Second one should be a pascal triangle...Not an extension of the first one.
    Haven't worked on third triangle yet, but it's to be an upside down pascal triangle.

    Would using scanner class be coded in this one or in the class Triangle?

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Padding with blanks

    For the Pascal triangles (Note for Forum: Pascal triangles are pirmide shaped), you need to pad the output with blanks...
    • For the upright pascal triangle, the number of blanks are per line decrease as the triangle grows

    • For the upside down pascal triangle, the number of blanks increases as the triangle decreases


    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    CodeDog is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default

    Not sure I am following you..Could you give an example?

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default padding with blanks

    I was thinking something like (pseudo code):

    System.out.println(" " + pascalPiramideNumber);//4 blanks before number
    System.out.println(" " + pascalPiramideNumber);//3 blanks before 3 numbers
    System.out.println(" " + pascalPiramideNumber);//2 blanks before 5 numbers
    System.out.println(" " + pascalPiramideNumber);//1 blanks before 7 numbers
    System.out.println("" + pascalPiramideNumber);//0 blanks before 9 numbers
    etc..
    • To build it, you would have to decrement the blank spaces and increment the rows of numbers through a loop.
    • Just do the reverse with the inverted Pascal piramide.


    Hope this helps.
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    CodeDog is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default

    OK great thanks for your help, it's really appreciated.

  8. #8
    linux0kernel is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Default

    Java Code:
    #include <iostream.h>   
      
    void rectangle(int heigth, int width);   
      
    void isosceles(int heigth);   
      
    void diamond(int heigth);   
      
    int main()    
    {   
        int heigth;   
        int width;   
        char c;   
        cout<<"please choose the shapes that you would like using,if you want to quit please enter Q(uit)"<<endl;   
        cout<<"(R/I/D)"<<endl;   
        cin>>c;   
        if(c == 'R' || c == 'r')    
        {   
            cin>>heigth>>width;   
            rectangle(heigth, width);   
        }   
      
        if(c=='I' || c=='i')    
        {   
            cin>>heigth;   
            isosceles(heigth);   
        }   
      
        if(c=='D' || c=='d')    
        {   
            cout<<"Please enter an odd number!"<<endl;   
            cin>>heigth;   
            if((heigth%2) != 0)   
            {   
                diamond(heigth);   
            }   
            else    
            {   
                cout<<"This is an IllegalAgrument!"<<endl;   
            }   
        }   
        return 0;   
    }   
      
    void rectangle(int heigth, int width)    
    {   
        int i = 0;   
        int j = 0;   
        char c = '*';   
        for(i=0; i<heigth; i++)    
        {   
            for(j=0; j<width; j++)   
            {   
                cout<<c;   
            }   
            cout<<endl;   
        }   
    }   
      
    void isosceles(int heigth)   
    {   
        int i;   
        int j = 0;   
        int k = 0;   
        int row = heigth;   
        int spacenum;   
        int starnum;   
        for(k=0; k<heigth; k++)   
        {   
            spacenum = row--;   
            if(k==0)    
            {   
                starnum = 1;   
            } else    
            {   
                starnum = k*2+1;   
            }   
            for(i=spacenum; i>0; i--)    
            {   
                cout<<" ";   
            }   
            for(j=1; j<=starnum; j++)    
            {   
                cout<<"*";   
            }   
            cout<<endl;   
        }   
    }   
      
    void diamond(int heigth)    
    {   
        int rownum = heigth;   
        int spacenum;   
        int starnum;   
        int i=0;   
        int j=1;   
        int k;   
        int a = rownum/2;   
        for(i=0; i<heigth; i++)    
        {   
            if(i<rownum/2)   
            {   
                spacenum = a--;   
            }   
            else  
            {   
                spacenum = a++;   
            }   
            //cout<<spacenum<<"sapcenum"<<endl;   
            starnum = rownum -spacenum*2;   
            //cout<<starnum<<"starnum"<<endl;   
            for(j=0; j<spacenum; j++)    
            {   
                cout<<" ";   
            }   
            for(k=0; k<starnum; k++)    
            {   
                cout<<"*";   
            }   
            cout<<endl;   
        }   
    }

  9. #9
    linux0kernel is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Default

    Java Code:
    public class Number
    {
    	public static void main(String []s)
    	{
    		num_array  n=new num_array(3,2,false) ;
    		n.show() ;
    	}
    }
    class num_array
    {
    	private int a         ;//最多输出到几
    	private boolean order ;//正序还是倒序
    	private int distance  ;//表示出的时候的每一行之间数字个数之差 这个数必须大于或者等于0
    	
    	public num_array(int a,int distance,boolean order)//true表示正序 false表示倒序
    	{
    		this.a=a               ;
    		this.order=order       ;
    		this.distance=distance ;
    	}
    	public num_array()//默认的是最高输出到5,正序,每一行差一个数字
    	{
    		this.a=5               ;
    		this.order=true        ;
    		this.distance=3        ;
    	}
    	public void show()
    	{
    		if(order==true)
    		{
              if(distance==0)
    			for(int i=1;i<=a;i++)
    			System.out.println(i) ;
    		  if(distance>0)
    		  {
    		    for(int i=1;i<=a;i++)
    		    {
    		    	for(int j=1;j<=(i-1)*distance+1;j++)
                	System.out.print(i) ;
                	System.out.println() ;
    		    }
         	   }
    
    		}
    		if(order==false)
    		{
    			if(distance==0)
    				for(int i=a;i>=1;i--)
    		    	System.out.println(i) ;	
    		    if(distance>0)
    		    {
    		    	for(int i=a;i>=1;i--)
    		    	{
    		    	
    		    		for(int j=(i-1)*distance+1;j>=1;j--)
                    	System.out.print(i) ;
                    	System.out.println() ;
    		    	}
    		    }				
    		}
    	}
    }

  10. #10
    CodeDog is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

Similar Threads

  1. asterisks triangles
    By Dan121 in forum New To Java
    Replies: 1
    Last Post: 01-12-2008, 07:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •