Loops-Drawing a Tree (C++)

Apr 9, 2020 at 4:00pm
Hello. I am working on this assignment and it has been giving me serious grief. I was wondering if I may receive some help.

Instructions:

1. Modify the above 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, than 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 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. Note: Your program only needs to support ​odd-­numbered widths.

Step 1 was easy but now I am struggling with step 2. Nested loops have been hard for me to understand. When I run the code I get an infinite loop instead of the wanted trunk width. I am not sure where to go from here and have tried everything I can think of. I am not looking for the solution, just some hints that may help me get on the right track. Thank you so much.

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
  ##include <iostream>
#include <iostream>
using namespace std;

int main() {
    
    int TrunkHeight = 0;
    int TrunkWidth = 0;
    int i = 0;
    int b = 0;
    
    cout << " Enter trunk height: ";
    cin >> TrunkHeight;
    cout << " Enter trunk width: ";
    cin >> TrunkWidth;

    // Draw leaves
    cout << "  *  " << endl;
    cout << " *** " << endl;
    cout << "*****" << endl;
  
    
    
    for  (int i = 1; i <= TrunkHeight; i = i + 1) {
    
    cout <<  " ***" << endl;

    }
    
   for (int i = 1; TrunkHeight <= TrunkWidth; ++i) {
       for (int b = 1; b <= TrunkWidth; ++b)
       cout << "*";
   }
    
    
    return 0;
}
Last edited on Apr 9, 2020 at 4:01pm
Apr 9, 2020 at 4:56pm
its asking for the trunk in step 2.
so if you give it 3, I suspect they want to see

***
***
***
***

making a 'cylinder' tree trunk shape?

you can unroll that to a single loop by crafting a string of *s of the width given, and then printing it that many times.
cin trunklevels and trunkwidth
for(... trunkwidth)
stringvar += "*"
for(... trunklevels)
cout stringvar

and you won't need to nest the loops at all.
but the nested loop logic is
for(trunk levels)
{ //necessary to make a block for outer loop which will be a for loop and a print statement.
for (trunk width)
cout * //inner loop writes the *s

cout endl; //outer loop writes the end of lines
}

Apr 9, 2020 at 5:28pm
Thank you! This helps so much! Also thanks for the single loop tip, I will keep that in mind!
Topic archived. No new replies allowed.