Help making a tree out of asterisks

I was able to write a code to make a triangle that looks like this:
*
**
***
****

1
2
3
4
5
6
7
8
9
10
}else if(shape == 5){
    cout <<"\nHeight?: ";
    cin >> height;
    for(int a = 1; a <=height; a++){
      for(int b = 1;b<=a;b++){
        cout << "*";
      }
      cout << "\n";
    }
    cout << endl << endl;


But what I need now is to make something that looks like this, and I was not able to find it through searching. All the other 'trees' that people have made look different:
*
**
***
****
**
***
****
*****
***

It's supposed to be "half of a pine tree". I'm very new to C++, could I get some help?
Is bumping of our topics allowed? I still need help..
keep[ it simple, spot repetition, make it readable, the easier it is to read, the more you can focus on the problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

void drawTree(int min, int max)
{
	for (int stars = min; stars <= max; ++stars)
	{
		for (int star = 0; star < stars; ++star)
		{
			cout << "*";
		}
		cout << endl;
	}
}

int main()
{
    drawTree(1,4);
    drawTree(2,5);
    drawTree(3,3);
    return 0;
}


or make it massive :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
    int treeHeight = 20;
    int sectionHeight = 4;
    int sections = treeHeight / sectionHeight;
    
    for (int section = 1; section <= sections; ++section)
    {
        drawTree(section,sectionHeight+section);
    }
    drawTree(3,3);
    
//    drawTree(1,4);
//    drawTree(2,5);
//    drawTree(3,3);
    return 0;
}
Last edited on
That code works for the pattern of the tree, but I need it to match the style of the code I initially posted. I need to ask the user for a height, and then output stars based on the conditions of the two for loops, and the pattern needs to be correct no matter what height the user gives me.
Right now I have this:
1
2
3
4
5
6
7
8
9
10
}else if(shape == 7){
    cout <<"\nHeight?: ";
    cin >> height;
    for(int a = 0;a < height;a++){
      for(int b = 0;b < (a % 4) + (a / 4);b++){
        cout << "*";
      }
      cout << "\n";
    }
    cout << endl << endl;

Which ALMOST worked, but the pattern is slightly off. Could I have some help fixing it? I've been changing it around trying to get it right, but everything I've tried hasn't worked.
Last edited on
Topic archived. No new replies allowed.