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;
}
|