419 Program 5 Loops Drawing a Tree
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?
| |
Last edited on
Hello student2019,
I do not have a full answer yet, but I am thinking it has something to do with line 32 and the 0.25. When I tried the 5 3 5 that did not work and changed 0.25 to 0.125 it worked and looked proper. Closer to the left edge of the screen, but proper form.
There could also be a proper set of numbers for trunk width and leaves width i.e., the width of the trunk needs the proper ratio to the width of the leaves.
Hopes that helps,
Andy
Hello student2019,
The brain finally kicked in a bit and I had this idea that seems to work, Give it a try if you want.
| |
Also on line 9 I changed int a to a double because leavesWidth was upgraded to a double for the calculation and the comparison was between an int and a double or at least a float.
Hope that helps,
Andy
Thank you so much! It makes more sense now
williamsvaink1945.blogspot.com
Source: https://www.cplusplus.com/forum/beginner/200798/
0 Response to "419 Program 5 Loops Drawing a Tree"
Post a Comment