473,398 Members | 2,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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

Expand|Select|Wrap|Line Numbers
  1. #include <iostream> // using the main library
  2. using namespace std;
  3.  
  4.  
  5.  
  6. // Function declarations
  7.  
  8. char this_char () ;
  9. int get_num_row () ;
  10. void draw_tri ( char this_char, int num_row ) ;
  11.  
  12. int main ()
  13. {
  14.     char this_char = '*' ;
  15.  
  16.     //Get number of rows.
  17.     int num_row ;
  18.     num_row = get_num_row () ;
  19.  
  20.     // Draw triangles
  21.     draw_tri ( this_char, num_row ) ;
  22.  
  23.     return 0 ;
  24.  
  25. }
  26.  
  27.  
  28. // Function to get number of rows.
  29.  int get_num_row ()
  30. {
  31.     int num_row ;
  32.     cout << " Please enter the number of rows you would like in your triangle. " << endl ;
  33.     cin >> num_row ;
  34.  
  35.     return num_row ;
  36.  
  37. }
  38.  
  39. // Function to draw triangles.
  40. void draw_tri (char this_char, int num_row ) 
  41. {
  42.     for ( int i = 1 ; i <= num_row ; i ++ )
  43.     {                     // Nested loop.
  44.         for ( int j = 1 ; j <= i ; j ++ )
  45.         {
  46.             cout << " * " ;
  47.  
  48.         }
  49.         cout << endl << endl ;
  50.  
  51.     }
  52. }
Oct 23 '07 #1
5 2476
amitpatel66
2,367 Expert 2GB
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

Expand|Select|Wrap|Line Numbers
  1. #include <iostream> // using the main library
  2. using namespace std;
  3.  
  4.  
  5.  
  6. // Function declarations
  7.  
  8. char this_char () ;
  9. int get_num_row () ;
  10. void draw_tri ( char this_char, int num_row ) ;
  11.  
  12. int main ()
  13. {
  14.     char this_char = '*' ;
  15.  
  16.     //Get number of rows.
  17.     int num_row ;
  18.     num_row = get_num_row () ;
  19.  
  20.     // Draw triangles
  21.     draw_tri ( this_char, num_row ) ;
  22.  
  23.     return 0 ;
  24.  
  25. }
  26.  
  27.  
  28. // Function to get number of rows.
  29.  int get_num_row ()
  30. {
  31.     int num_row ;
  32.     cout << " Please enter the number of rows you would like in your triangle. " << endl ;
  33.     cin >> num_row ;
  34.  
  35.     return num_row ;
  36.  
  37. }
  38.  
  39. // Function to draw triangles.
  40. void draw_tri (char this_char, int num_row ) 
  41. {
  42.     for ( int i = 1 ; i <= num_row ; i ++ )
  43.     {                     // Nested loop.
  44.         for ( int j = 1 ; j <= i ; j ++ )
  45.         {
  46.             cout << " * " ;
  47.  
  48.         }
  49.         cout << endl << endl ;
  50.  
  51.     }
  52. }
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??
Oct 23 '07 #2
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
Oct 23 '07 #3
amitpatel66
2,367 Expert 2GB
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:

Expand|Select|Wrap|Line Numbers
  1. cout<< "Enter no of rows required:";
  2. cin>>b;
  3. cout<< "Enter total no of triangles required:";
  4. cin >> a;
  5. for(int i=1;i<=a;i++)
  6. {
  7.  draw_tri(b);
  8. }
  9.  
Hope this helps!!
Oct 23 '07 #4
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

Expand|Select|Wrap|Line Numbers
  1. #include <iostream> // using the main library
  2. using namespace std;
  3.  
  4.  
  5.  
  6. // Function declarations
  7.  
  8. char this_char () ;
  9. int get_num_tri () ;
  10. int get_num_row () ;
  11. void draw_tri ( int num_tri, int num_row ) ;
  12.  
  13. int main ()
  14. {
  15.  
  16.     // Get number of triangles
  17.     int num_tri ;
  18.     num_tri = get_num_tri () ;
  19.  
  20.     // Get number of rows
  21.     int num_row ;
  22.     num_row = get_num_row () ;
  23.  
  24.     // Draw triangles
  25.     draw_tri ( num_tri, num_row ) ;
  26.  
  27.     return 0 ;
  28.  
  29. }
  30.  
  31. // Funtion to get user to input number of triangles and rows.
  32. int get_num_tri ()
  33. {
  34.     int num_tri ;
  35.     int num_row ;
  36.     cout << " Please enter the number of triangles you would like " << endl ;
  37.     cin >> num_tri ;
  38.     cout << " Please enter the number of rows you would like " << endl ;
  39.     cin >> num_row ;
  40.     return num_tri ;
  41.     return num_row ;
  42. }
  43.  
  44.  
  45.  
  46. // Function to draw triangles.
  47. void draw_tri ( int num_tri, int num_row ) 
  48. {
  49.     for ( int i = 1 ; i <= num_tri ; i ++ )
  50.     {
  51.      draw_tri (num_row) ;
  52.     for (int j = 1 ; j <= num_row ; j ++ )   
  53.         {
  54.             cout << " * " ;
  55.  
  56.         }
  57.         cout << endl << endl ;
  58.     }
  59.  
  60.  
  61. }
Oct 23 '07 #5
amitpatel66
2,367 Expert 2GB
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

Expand|Select|Wrap|Line Numbers
  1. #include <iostream> // using the main library
  2. using namespace std;
  3.  
  4.  
  5.  
  6. // Function declarations
  7.  
  8. char this_char () ;
  9. int get_num_row () ;
  10. void draw_tri ( char this_char, int num_row ) ;
  11.  
  12. int main ()
  13. {
  14.     char this_char = '*' ;
  15.  
  16.     //Get number of rows.
  17.     int num_row ;
  18.     num_row = get_num_row () ;
  19.  
  20.     // Draw triangles
  21.     draw_tri ( this_char, num_row ) ;
  22.  
  23.     return 0 ;
  24.  
  25. }
  26.  
  27.  
  28. // Function to get number of rows.
  29.  int get_num_row ()
  30. {
  31.     int num_row ;
  32.     cout << " Please enter the number of rows you would like in your triangle. " << endl ;
  33.     cin >> num_row ;
  34.  
  35.     return num_row ;
  36.  
  37. }
  38.  
  39. // Function to draw triangles.
  40. void draw_tri (char this_char, int num_row ) 
  41. {
  42.     for ( int i = 1 ; i <= num_row ; i ++ )
  43.     {                     // Nested loop.
  44.         for ( int j = 1 ; j <= i ; j ++ )
  45.         {
  46.             cout << " * " ;
  47.  
  48.         }
  49.         cout << endl << endl ;
  50.  
  51.     }
  52. }
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:

Expand|Select|Wrap|Line Numbers
  1. int main ()
  2. {
  3.     char this_char = '*' ;
  4.     int no_of_tri = 1;
  5.  
  6.     //Get number of rows.
  7.     int num_row ;
  8.     num_row = get_num_row () ;
  9.  
  10.    //Get no of triangles required
  11.   cout<< "Enter no of triangles required:";
  12.   cin >> no_of_tri;
  13.  
  14.     // Draw triangles
  15.     for(int i=1;i<=no_of_tri;i++)
  16.    {
  17.     draw_tri ( this_char, num_row ) ;
  18.    }
  19.  
  20.     return 0 ;
  21. }
  22.  
Oct 23 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: javadkhan | last post by:
Hi All, I am trying to supress the small black triangle that shows up in the menus meaning the menuitem is parent. The reason for that is I drew my own 3D looking triangle in DrawItem using...
0
by: lottoman | last post by:
Hello all, I am trying to draw a triangle on a toolbarbutton to simulate a drop down button without having the extra button beside my drop downbutton. I was successfully able to draw the...
5
by: singhm | last post by:
Hi guys so I have a trianlge program having hard time finishing this though, I have to develop a program which is the following: Write a program that will allow the user to enter the 3 lengths...
12
by: asif929 | last post by:
I am trying to write a program which creates four triangles. The program begins with prompting a user " Enter the size of triangles", number from 1 to N is the size of four triangles For Example if...
0
geo039
by: geo039 | last post by:
I have a program that takes user input from a textbox. Based on those 3 numbers it will tell them whether it is a right triangle, equilateral triangle or not a triangle. I've written 3 constructors...
1
by: vykkilynn | last post by:
My homework this week involved using the for statement to create loops that would display triangles. The only output we're allowed to use are forms of the print statements... i.e. print(),...
19
by: lost1 | last post by:
Can someone point me in the right direction on how to get the triangle type to display. Below is a triangle class that is tested by another completely separate class. The main method of the test...
6
by: jackj | last post by:
Hi, I am first time C++ student and doing the usual tasks. This one is to create a triangle based on user input of how large (how many rows) and what symbol to use. I have managed to create a...
3
by: abraxas91 | last post by:
Hi, my name is Stephan. I've been working on this program off and on over the past few days. I have yet to perfect it. I would really appreciate any help! For some reason the output begins to...
4
by: milk242 | last post by:
I am trying to figure out how to print a triangle in the shape of this depending on the user input. If they input 4 the max height would be 4. * ** *** **** *** ** * I have no idea how to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.