program that creates a custom-sized triangle

Write your question here.
Okay, so I am trying to figure out how to create a program that will draw a triangle using *'s with a base the has a user-inputted number of *'s like so:

*
***
*****

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 int width = 0;
	int height = 0;
	int i = 0;
	int leafWidth = 0;
	int i2 = 1;
	int i3 = 1;
	int i4 = 1;

	cout << "Enter trunk height: " << endl;
	cin >> height;
	cout << "Enter trunk width: " << endl;
	cin >> width;
	cout << "Enter leaves width: " << endl;
	cin >> leafWidth;
	cout << endl;
	i2=1;
	i3=1;
	i4=1;
	while (i2<=leafWidth){
		while (i3 <=i4){
			cout << "*";
			i3=i3+1;
			i4=i4+2;
		}
		cout << endl;

		i2 = i2+2;
	}
	


Any pointers on how to make this work, or even pointers on what I am doing wrong would be really, really helpful. Anything to get some momentum. Thank you
I'm not sure what you're trying to do with 4 "i" variables. Also, you didn't explain what you mean by "leaves" width. Ignoring those parts, it might be easier for you to figure out how to just do "45-45-90" triangle, meaning the height and width are the same.

Here's the simplest way (not using recursion) to do that:
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
    const int WIDTH = 5;
    for (int i = 1; i <= WIDTH; i++)
    {
        for (int j = 1; j <= i; j++)
        {
            std::cout << "*";
        }
        std::cout << std::endl;
    }
}

Every inner loop, it prints asterisks up to what the number of the outer loop is, and every outer loop, it prints a newline, one for each row needed.

The problem with having an independent height and width is that the increase in stars each time will have to be calculated by the slope of the triangle. For example, in the above code, a 45-45-90 triangle's hypotenuse would have a slope of 1, it prints +1 asterisk for every +1 row.

But if the width was 2x the height, the slope of the triangle would be 1/2, and it would need to print out +2 asterisks each row to account for this. So, somewhere in your code, you'd need to calculate the slope (rise/run, height/width) and then use that to calculate how many more asterisks you need to bring for each row so that the final row will have the right amount printed... this could be hard because you're only dealing with integers, without thinking about code, what would a triangle with height 10, width 4 look like?
Last edited on
OK, I think I did a great job at butchering the explanation. I am trying to create a program that will code in a pyramid (something more along the lines of a equilateral triangle) with a base that matches a user-inputted integer (i.e. user inputs 6, so the base has 6 *'s and the peak only has 1). I believe I need to create a loop to print the correct number of whitespaces (so that it is properly aligned at the peak) and the correct number of *'s on each line. Part of the reason I have so many i variables is because I have no idea what I am doing. I will try to illustrate the basic form I want to achieve.

*
***
*****

Thanks for responding. I'm gonna study your code to see if it gives me any ideas.
Topic archived. No new replies allowed.