Loops - Drawing a Tree (c++)

Hi! I'm having trouble with this problem. I was wondering what would be the best way to do this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  #include <iostream>
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.

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.

1
2
3
4
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.

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.
Thank you!
.
Last edited on
So which of tasks (1), (2), (3), (4) are you having problems with?

Start from task (1)
- ask the user to specify a number of tree trunk levels (hint: use cout to write what your are told and cin to get user input)
- use a for() loop to draw that many levels, instead of the 4 by default.

Do this first!
@lastchance

so far I have this much done:

 
code removed


I'm mostly confused on the leavesWidth part
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main ()
{
	int i, j;
	int trunkHeight = 0;
	int trunkWidth = 0;
	int leavesWidth = 0;
  
	cout << "Enter leaves width: ";
	cin >> leavesWidth;
	cout << endl;

	for(i = 0; i < leavesWidth * 2; i += 2)
	{
		for(j = (leavesWidth * 2 - i) / 2; j >= 0; j--) cout << " ";
		for(j = 0; j <= i; j++) cout << "*";

		cout << endl;		
	}

	return 0;
}
Enter leaves width: 18

                   *
                  ***
                 *****
                *******
               *********
              ***********
             *************
            ***************
           *****************
          *******************
         *********************
        ***********************
       *************************
      ***************************
     *****************************
    *******************************
   *********************************
  ***********************************
Press any key to continue
Last edited on
@SakurasouBusters
is "i += 2" another way to type i= i+2? I haven't learned that yet
Yes.
alright, thank you so much!
Topic archived. No new replies allowed.