Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

c++ Write a function called treeTop that prints the top of a tree. It should tak

ID: 3593318 • Letter: C

Question

c++

Write a function called treeTop that prints the top of a tree. It should take no parameters and return void. Write a second function called treeTrunk that prints the trunk of a tree. It should take one integer as a parameter for height (how many lines tall the trunk is), and also have a return type of void. Finally, create a function called drawTrees that takes two integer parameters, one for height and one for the number of trees to draw. drawTrees should use treeTop and treeTrunk.

Have the user enter the height of the trees (same for all) and number of trees to draw. Remember that to draw a backslash you need two backslashes.

Test cases:

Test case 1:

Enter tree height: <2>

Enter number of trees: <3>

    ^

   /

/    

/____

    | |

    | |

    ^

   /

/    

/____

    | |

    | |

    ^

   /

/    

/____

    | |

    | |

Test case 1:

Enter tree height: <5>

Enter number of trees: <1>

    ^

   /

/    

/____

    | |

    | |

    | |

    | |

    | |

Explanation / Answer

Code:

#include <iostream>

using namespace std;

//Method Declarations
void treeTop();
void treeTrunck(int);
void drawTrees(int, int);

//Global variable to store height and number of trees;
int height,noOfTrees;
int main(){
  
   //Taking the input from the user
   cout << "Enter tree height: " << endl;
   cin >> height;
   cout << "Enter number of trees: " << endl;
   cin >> noOfTrees;
  
   //drawTrees method calling
   drawTrees(height, noOfTrees);
   return 0;
}

void treeTop(){
  
   //Printing top of the tree
   cout << "    ^ ";
   cout << "   / \ ";
   cout << " /   \ ";
   cout << " /     \ ";
   cout << " ----- ";
}

void treeTrunk(int h){
  
   //Printing the Trunk of the tree
   //Printing "||" height times
   int i;
   for(i=1;i<=h;i++){
       cout << "   || ";
   }
}

void drawTrees(int h, int n){
  
   //Printing the tree number of tree times
   int i;
   for(i=1;i<=n;i++){
      
       //Every time printing top of tree and trunk of tree and new line
       treeTop();
       treeTrunk(h);
       cout << endl;
   }
}

Output 1:

Enter tree height:
2
Enter number of trees:
3
    ^
   /
/  
/    
-----
   ||
   ||

    ^
   /
/  
/    
-----
   ||
   ||

    ^
   /
/  
/    
-----
   ||
   ||

output 2:

Enter tree height:
5
Enter number of trees:
1
    ^
   /
/  
/    
-----
   ||
   ||
   ||
   ||
   ||

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote