(1) Modify the above program to ask the user to specify a number of tree trunk l
ID: 3695003 • Letter: #
Question
(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, 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.
Here is an example program execution (user input is in italics it for clarity):
Here is an example program execution (user input is highlighted here for clarity). Note that the trunk is misaligned with the leaves when the trunk and leaves widths are neither both odd nor both even:
Here is an example program execution (user input is highlighted here for clarity). Note that the top of the leaves is misaligned when the leaves width is even.:
Explanation / Answer
//The following draws tree for only odd number for trunk width and odd number for number of leaves.
Program:
#include <iostream>
using namespace std;
int main() {
int trunk_height,trunk_width,leaves_width,spaces;
cout<<"Enter trunk height: "<<endl;
cin>>trunk_height;
cout<<"Enter trunk width: "<<endl;
cin>>trunk_width;
while ((trunk_width % 2) == 0) {
cout << "Please enter an odd number for the width"<<endl;
cin >> trunk_width;
}
cin>>leaves_width;
cout<<"Enter leaves width: "<<endl;
cin>>leaves_width;
while ((leaves_width % 2) == 0) {
cout << "Please enter an odd number for the width : "<<endl;
cin >> leaves_width;
}
// Draw leaves
for(int i=1;i<=leaves_width;i=i+2)
{
spaces=leaves_width-i;
for(int j=0;j<spaces/2;j++)
cout<<" ";
for(int j=0;j<i;j++)
cout<<"*";
for(int j=0;j<spaces/2;j++)
cout<<" ";
cout<<endl;
}
// Draw trunk
for(int i=1;i<=trunk_height;i++)
{
spaces=leaves_width-trunk_width;
for(int j=0;j<spaces/2;j++)
cout<<" ";
for(int j=0;j<trunk_width;j++)
cout<<"*";
for(int j=0;j<spaces/2;j++)
cout<<" ";
cout<<endl;
}
return 0;
}
Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.