Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write a program to print the pattern of a simple tree. This tree has two parts, including the treetop and the trunk. After the
Write a program to print the pattern of a simple tree. This tree has two parts, including the treetop and the trunk. After the program starts, the user inputs an integer value (data type: int) S, representing the treetop's width, i.e., the number of "*" in the last row of the treetop. We assume that S is an odd number and is no smaller than 5. So, the program needs to check the correctness of this input. If it is invalid, the user needs to input another S value again. When the input S is valid, print the tree according to the following requirements: The treetop is an isosceles triangle composed of S-1 rows, and each row has an increasing odd number of "*" starting from 1. The trunk is a hollow rectangle with the width S - 4 and the height [S/2] + 1. When the width is greater than 1, only the left-most and the right-most columns are printed. After the valid S is provided, the user needs to further input one character (data type: char) D. We assume D is correct, i.e., no need to check its correctness. If D is either 'u' or 'U', please print the tree in a normal way. IfD is either 'r' or 'R', please print the tree in a reversed way, i.e., from truck to the treetop. The shape of each part in the tree still follows the requirements above. Example 1: Please input the width of the treetop: 3 Invalid input! Please input the width of the treetop: 6 Invalid input! Please input the width of the treetop: 5 Please input the direction: U 3 Example 2: Please input the width of the treetop: 7 Please input the direction: u Example 3: Please input the width of the treetop: 9 Please input the direction: R
Step by Step Solution
★★★★★
3.50 Rating (153 Votes )
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started