triangle help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sugarflaps
    New Member
    • Oct 2007
    • 6

    triangle help

    Hi im needing a wee bit of help creating a series of triangles that get bigger each time the user has to input the number of triangles they want. I have managed to get my code to produce one triange but i cant get the loop to run for the number of triangles. Any help would be great thanks guys

    Code:
    #include <iostream> // using the main library
    using namespace std;
    
    
    
    // Function declarations
    
    char this_char () ;
    int get_num_row () ;
    void draw_tri ( char this_char, int num_row ) ;
    
    int main ()
    {
    	char this_char = '*' ;
    
    	//Get number of rows.
        int num_row ;
        num_row = get_num_row () ;
    
    	// Draw triangles
    	draw_tri ( this_char, num_row ) ;
    
    	return 0 ;
    
    }
    
    
    // Function to get number of rows.
     int get_num_row ()
    {
    	int num_row ;
    	cout << " Please enter the number of rows you would like in your triangle. " << endl ;
    	cin >> num_row ;
    
    	return num_row ;
    
    }
    
    // Function to draw triangles.
    void draw_tri (char this_char, int num_row ) 
    {
    	for ( int i = 1 ; i <= num_row ; i ++ )
    	{                     // Nested loop.
    		for ( int j = 1 ; j <= i ; j ++ )
    		{
    			cout << " * " ;
    			
            }
    		cout << endl << endl ;
    		
    	}
    }
  • amitpatel66
    Recognized Expert Top Contributor
    • Mar 2007
    • 2358

    #2
    Originally posted by sugarflaps
    Hi im needing a wee bit of help creating a series of triangles that get bigger each time the user has to input the number of triangles they want. I have managed to get my code to produce one triange but i cant get the loop to run for the number of triangles. Any help would be great thanks guys

    Code:
    #include <iostream> // using the main library
    using namespace std;
    
    
    
    // Function declarations
    
    char this_char () ;
    int get_num_row () ;
    void draw_tri ( char this_char, int num_row ) ;
    
    int main ()
    {
    	char this_char = '*' ;
    
    	//Get number of rows.
        int num_row ;
        num_row = get_num_row () ;
    
    	// Draw triangles
    	draw_tri ( this_char, num_row ) ;
    
    	return 0 ;
    
    }
    
    
    // Function to get number of rows.
     int get_num_row ()
    {
    	int num_row ;
    	cout << " Please enter the number of rows you would like in your triangle. " << endl ;
    	cin >> num_row ;
    
    	return num_row ;
    
    }
    
    // Function to draw triangles.
    void draw_tri (char this_char, int num_row ) 
    {
    	for ( int i = 1 ; i <= num_row ; i ++ )
    	{                     // Nested loop.
    		for ( int j = 1 ; j <= i ; j ++ )
    		{
    			cout << " * " ;
    			
            }
    		cout << endl << endl ;
    		
    	}
    }
    Hi,

    get the input from the user the no of triangles he want to draw, then run a for loop using that value from 1...9( say user wants to draw 9 triangles) and with in the loop call draw_tri function one time each for every value of for loop. so the draw_tri function will be called 9 times here!!
    Is that simple??

    Comment

    • sugarflaps
      New Member
      • Oct 2007
      • 6

      #3
      Originally posted by amitpatel66
      Hi,

      get the input from the user the no of triangles he want to draw, then run a for loop using that value from 1...9( say user wants to draw 9 triangles) and with in the loop call draw_tri function one time each for every value of for loop. so the draw_tri function will be called 9 times here!!
      Is that simple??

      Hi im not quite sure what you mean do i get rid of rows and just go with thenumber of triangles. thanks

      Comment

      • amitpatel66
        Recognized Expert Top Contributor
        • Mar 2007
        • 2358

        #4
        Originally posted by sugarflaps
        Hi im not quite sure what you mean do i get rid of rows and just go with thenumber of triangles. thanks
        No. I mean you get two input values from the user:
        1. No of rows required.
        2. No of triangle

        Call draw_tri functrion passing no of rows as input to that function as you are doing that much no of times as mentioned by user.

        Eg:
        sample code snippet:

        [code=c]
        cout<< "Enter no of rows required:";
        cin>>b;
        cout<< "Enter total no of triangles required:";
        cin >> a;
        for(int i=1;i<=a;i++)
        {
        draw_tri(b);
        }
        [/code]

        Hope this helps!!

        Comment

        • sugarflaps
          New Member
          • Oct 2007
          • 6

          #5
          hi is this what you mean i keep getting this error message error C2660: 'draw_tri' : function does not take 1 arguments
          here i my ammended code

          Code:
          #include <iostream> // using the main library
          using namespace std;
          
          
          
          // Function declarations
          
          char this_char () ;
          int get_num_tri () ;
          int get_num_row () ;
          void draw_tri ( int num_tri, int num_row ) ;
          
          int main ()
          {
          
              // Get number of triangles
          	int num_tri ;
          	num_tri = get_num_tri () ;
          
          	// Get number of rows
          	int num_row ;
          	num_row = get_num_row () ;
          
          	// Draw triangles
          	draw_tri ( num_tri, num_row ) ;
          
          	return 0 ;
          
          }
          
          // Funtion to get user to input number of triangles and rows.
          int get_num_tri ()
          {
          	int num_tri ;
          	int num_row ;
          	cout << " Please enter the number of triangles you would like " << endl ;
          	cin >> num_tri ;
          	cout << " Please enter the number of rows you would like " << endl ;
          	cin >> num_row ;
          	return num_tri ;
          	return num_row ;
          }
          
          
          
          // Function to draw triangles.
          void draw_tri ( int num_tri, int num_row ) 
          {
          	for ( int i = 1 ; i <= num_tri ; i ++ )
          	{
          	 draw_tri (num_row) ;
              for (int j = 1 ; j <= num_row ; j ++ )   
          		{
          			cout << " * " ;
          			
                  }
          		cout << endl << endl ;
          	}
          		
          	
          }

          Comment

          • amitpatel66
            Recognized Expert Top Contributor
            • Mar 2007
            • 2358

            #6
            Originally posted by sugarflaps
            Hi im needing a wee bit of help creating a series of triangles that get bigger each time the user has to input the number of triangles they want. I have managed to get my code to produce one triange but i cant get the loop to run for the number of triangles. Any help would be great thanks guys

            Code:
            #include <iostream> // using the main library
            using namespace std;
            
            
            
            // Function declarations
            
            char this_char () ;
            int get_num_row () ;
            void draw_tri ( char this_char, int num_row ) ;
            
            int main ()
            {
            	char this_char = '*' ;
            
            	//Get number of rows.
                int num_row ;
                num_row = get_num_row () ;
            
            	// Draw triangles
            	draw_tri ( this_char, num_row ) ;
            
            	return 0 ;
            
            }
            
            
            // Function to get number of rows.
             int get_num_row ()
            {
            	int num_row ;
            	cout << " Please enter the number of rows you would like in your triangle. " << endl ;
            	cin >> num_row ;
            
            	return num_row ;
            
            }
            
            // Function to draw triangles.
            void draw_tri (char this_char, int num_row ) 
            {
            	for ( int i = 1 ; i <= num_row ; i ++ )
            	{                     // Nested loop.
            		for ( int j = 1 ; j <= i ; j ++ )
            		{
            			cout << " * " ;
            			
                    }
            		cout << endl << endl ;
            		
            	}
            }
            DO NOT modify the code that you have written for generating one triangle.Just modify the MAIN function. I have made the changes as shown below. USe this main function for your program:

            [code=c]
            int main ()
            {
            char this_char = '*' ;
            int no_of_tri = 1;

            //Get number of rows.
            int num_row ;
            num_row = get_num_row () ;

            //Get no of triangles required
            cout<< "Enter no of triangles required:";
            cin >> no_of_tri;

            // Draw triangles
            for(int i=1;i<=no_of_tr i;i++)
            {
            draw_tri ( this_char, num_row ) ;
            }

            return 0 ;
            }
            [/code]

            Comment

            Working...