Question: Please write exact code to copy in C++ The following program draws a simple tree with leaves and a trunk. #include using namespace std; int

Please write exact code to copy in C++

The following program draws a simple tree with leaves and a trunk.

#include  using namespace std; int main() { // Draw rows of leaves cout << " *" << endl; cout << " ***" << endl; cout << "*****" << endl; // Draw rows of trunk cout << " ***" << endl; cout << " ***" << endl; cout << " ***" << endl; cout << " ***" << endl; return 0; } 

(1) Modify the above program to ask the user to specify a number for the height of the trunk; i.e. the number of tree trunk rows (Enter height of trunk: ). Make sure the value is positive. Use a loop to continue prompting until this condition is met.

Use a loop to draw that many trunk rows.

Testing suggestion: If the user specifies 4 tree trunk levels, then your code should draw a tree that looks the same as the tree produced by the original code.

(2) Modify the program again to ask the user to specify the width of the tree trunk, i.e. how many *s per row ("Enter width of trunk: ). Make sure this value is positive and is odd. Use a loop to continue prompting until both these conditions are met.

Youll 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.

The following code shows how to keep prompting the user until an odd number is entered. You will need to modify it, though, to also keep prompting until a positive number is entered.

while ((width % 2) == 0) { cout << "Please enter an odd number for width of trunk: "; cin >> width; cout << endl; } 

Testing suggestion: If the user specifies 4 tree trunk levels with a width of 3, then your code should draw a tree that looks the same as the tree produced by the original code.

(3) Modify the program again to ask the user to specify a width for the bottom row of leaves (Enter width of leaves: ). Make sure this value is greater than the trunk width the user previously entered + 1 and is odd. For example, if the user entered 5 for the trunk width, the leaves width should be greater than 6. Use a loop to continue prompting until both these conditions are met.

Use a nested loop to draw the leaves. The number of rows of leaves will depend on the leaves width entered. For example, if the leaves width is 5, there will be three rows of leaves; the first row will have one *, the second row, will have 3 *'s, and the third row will have 5 *'s. Youll 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.

You will need two inner loops for drawing each line of leaves: one for outputting spaces and one for outputting *s.

You will also need to modify the code that prints the tree trunk so that the trunk is centered under the last line of leaves.

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

Here is an example program execution. The user typed inputs have asterisks around them to clearly denote them as user inputs.

Enter height of trunk: *10* Enter width of trunk: *5* Enter width of leaves: *11* * *** ***** ******* ********* *********** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** 

Here is an example program execution when the user attempts to enter an even number for trunk width. A similar pattern should be followed, when a user attempts to enter an even number for leaves width as well. The user typed inputs have asterisks around them to clearly denote them as user inputs.

Enter height of trunk: *3* Enter width of trunk: *4* Please enter an odd number for width of trunk: *2* Please enter an odd number for width of trunk: *6* Please enter an odd number for width of trunk: *3* Enter width of leaves: *7* * *** ***** ******* *** *** ***

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!