Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

6.1 PROGRAM 5: Loops - Drawing a Tree, C++ #include using namespace std; int main() { // Draw leaves cout < < * <

6.1 PROGRAM 5: Loops - Drawing a Tree, C++

#include using namespace std;

int main() {

// Draw leaves cout << " *" << endl; cout << " ***" << endl; cout << "*****" << endl;

// Draw trunk cout << " ***" << endl; cout << " ***" << endl; cout << " ***" << endl; cout << " ***" << endl;

return 0; }

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

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.

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

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

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

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.

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. The user typed inputs have asterisks around them to clearly denote them as user inputs.

Enter trunk height: *10* Enter trunk width: *5* Enter leaves width: *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 trunk height: *3* Enter trunk width: *4* Please enter an odd number for the width: *2* Please enter an odd number for the width: *6* Please enter an odd number for the width: *3* Enter leaves width: *7* * *** ***** ******* *** *** ***

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Current Trends In Database Technology Edbt 2006 Edbt 2006 Workshops Phd Datax Iidb Iiha Icsnw Qlqp Pim Parma And Reactivity On The Web Munich Germany March 2006 Revised Selected Papers Lncs 4254

Authors: Torsten Grust ,Hagen Hopfner ,Arantza Illarramendi ,Stefan Jablonski ,Marco Mesiti ,Sascha Muller ,Paula-Lavinia Patranjan ,Kai-Uwe Sattler ,Myra Spiliopoulou ,Jef Wijsen

2006th Edition

3540467882, 978-3540467885

More Books

Students also viewed these Databases questions

Question

What must a creditor do to become a secured party?

Answered: 1 week ago

Question

When should the last word in a title be capitalized?

Answered: 1 week ago

Question

LO4 Specify how to design a training program for adult learners.

Answered: 1 week ago