Loops - Drawing a Tree (c++)

Hi! I'm having trouble with this problem.
(1) Modify the program to ask the user to specify a number of tree trunk levels (“Enter trunk height: “), then use a loop to draw that many levels.

Testing suggestion: If the user specifies 4 tree trunk levels, then the original tree should be drawn.

(2) Modify the program again to ask the user to specify a number of tree trunk *’s per level ("Enter trunk width: “), then use a loop to draw that many *’s per level.

You’ll need to use a nested loop in which the inner loop draws the *’s, and the outer loop iterates a number of times equal to the number of tree trunk levels.

(3) Modify the program so that it only accepts odd numbers for the trunk width. Use a loop to continue prompting the user for a width until the number is odd.

(4) Modify the program to ask the user to specify a number of tree leaves levels (“Enter leaves width: “), then use a nested loop to draw that many levels.

You’ll need two inner loops for drawing the leaves: one for outputting spaces and one for outputting *’s. The outer loop iterates a number of times equal to the number of tree leaves levels.

The top level of the leaves must have at least one *.

You will have to modify this as well to only accept odd numbers for the number of leaves.
_________
So this is what I have done so far. It is correct for 2 sets of inputs (10,5,11 and 3, 3,7) however, it is incorrect for another which is: 5, 3, 5 - the trunk is not centered to the leaves. I was wondering how to fix this?

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
using namespace std;

int main (){
   int trunkHeight = 0;
   int trunkWidth = 0;
   int leavesWidth = 0;
  
   cout << "Enter trunk height: ";
   cin >> trunkHeight;
   cout << "Enter trunk width: ";
   cin >> trunkWidth; 
   while ((trunkWidth % 2) == 0) {
    cout << "Please enter an odd number for the width: ";
    cin >> trunkWidth;
}
   cout << "Enter leaves width: ";
   cin >> leavesWidth;
   cout << endl;
   
   for(int c = 1; c <= leavesWidth; c = c + 2){
       for(int d = 0; d < (leavesWidth - c) / 2; ++d){
           cout << " ";
       }
       for(int e = 0; e < c; e++){
           cout << "*";
       }
       cout << endl;
   }
   
   for (int i = 0; i < trunkHeight; ++i){
      for (int a = 0; a < leavesWidth * (0.25) ; ++a){
      cout << " ";
      }
      for (int b = 0; b < trunkWidth; ++b){
      cout << "*"; 
      }
      cout << endl;
   } 

   return 0;
}
   
Last edited on
Hello student2019,

I do not have a full answer yet, but I am thinking it has something to do with line 32 and the 0.25. When I tried the 5 3 5 that did not work and changed 0.25 to 0.125 it worked and looked proper. Closer to the left edge of the screen, but proper form.

There could also be a proper set of numbers for trunk width and leaves width i.e., the width of the trunk needs the proper ratio to the width of the leaves.

Hopes that helps,

Andy
Hello student2019,

The brain finally kicked in a bit and I had this idea that seems to work, Give it a try if you want.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
double x{ 0.0 };
	 
if (leavesWidth % trunkWidth > 1)
	x = 0.125;
else
	x = 0.25;

for (int i = 0; i < trunkHeight; ++i) {
	for (double a = 0; a < leavesWidth * x; ++a) {
		cout << " ";
	}
	for (int b = 0; b < trunkWidth; ++b) {
		cout << "*";
	}
	cout << endl;
}


Also on line 9 I changed int a to a double because leavesWidth was upgraded to a double for the calculation and the comparison was between an int and a double or at least a float.

Hope that helps,

Andy
Thank you so much! It makes more sense now
Topic archived. No new replies allowed.