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