Write a C++ program that draws either a hollow triangle or a hollow diamond using a user-specified symbol. The user enters three values:
shape height symbol
where
shape is T, t, D, or d.
T or t: draw a hollow triangle.
D or d: draw a hollow diamond.
height is an odd integer from 5 to 50.
symbol is the character used to draw the shape.
Your program should work for any valid height. Do not hard-code the output for a particular height.
Example 1 (Hollow Triangle)
Enter a shape (T for triangle or D for diamond),
the height (as an odd integer),
and the symbol (such as o, x, or *): t 5 *
*
* *
* *
* *
*********
Example 2 (Hollow Triangle)
Enter a shape (T for triangle or D for diamond),
the height (as an odd integer),
and the symbol (such as o, x, or *): T 7 o
o
o o
o o
o o
o o
o o
ooooooooooooo
Example 3 (Hollow Diamond)
Enter a shape (T for triangle or D for diamond),
the height (as an odd integer),
and the symbol (such as o, x, or *): D 5 o
o
o o
o o
o o
o
Example 4 (Hollow Diamond)
Enter a shape (T for triangle or D for diamond),
the height (as an odd integer),
and the symbol (such as o, x, or *): d 7 *
*
* *
* *
* *
* *
* *
*
It is recommended that you modify the code template to complete this assignment.
Important: Empty, Space, and Newline
In this assignment, empty, space, and newline have different meanings.
Symbol: the character used to draw the shape, such as o, x, or *.
Space: an actual space character, ' ', printed by the program.
Empty: nothing is printed. An empty position does not contain a space.
Newline: the end of the current output row. In C++, this can be produced by '\n' or endl.
For example, the first row of a triangle of height 5 is:
*
Its output consists of:
space space space space * newline
There are no spaces after *. After the last symbol on a row, the program should immediately print a newline.
Similarly, the second row of a triangle of height 5 is:
* *
consists of:
space space space * space * newline
Therefore: empty ≠ space ≠ newline
Do not add spaces merely to make every output row have the same number of printed characters.
Hint for the Hollow Triangle
Think of each shape one row at a time. For everyrow ask:
How many spaces should be printed before the first symbol?
Where is the left boundary of the shape?
Where is the right boundary of the shape?
How many spaces, if any, are between the two boundary symbols?
When should the row end with a newline?
Consider a triangle of height 5.
column
0 1 2 3 4 5 6 7 8
row 0 *
row 1 * *
row 2 * *
row 3 * *
row 4 * * * * * * * * *
Focus first on the positions of the two sloping sides:
row left side right side
0 4 4
1 3 5
2 2 6
3 1 7
4 0 8
Now look at the pattern carefully. As the row number increases:
left side: 4, 3, 2, 1, 0
right side: 4, 5, 6, 7, 8
You can also think about the number of spaces printed on each row. For example:
row 0: 4 leading spaces, *, newline
row 1: 3 leading spaces, *, 1 inside space, *, newline
row 2: 2 leading spaces, *, 3 inside spaces, *, newline
row 3: 1 leading space, *, 5 inside spaces, *, newline
row 4: 9 symbols, newline
Observe the above, you obtain the pattern for the number of spaces between the two boundary symbols.
Refer to this figure for guidance on drawing. (Note: "Empty" is not equivalent to a space.)
The height is an integer between 5 and 50.
You do not need to validate the inputs.
In Example 1, the user types "t<space>5<space>*" and then hits the "Enter" key.
It is recommended that you modify the code template to complete this assignment.
The file size of your program should be less than 10 KB.
The outputs of your program should be exactly the same as those in the the previous examples.
Every output character (spaces, newlines, punctuation, and letters) of your program should be identical to those in the input-output samples.
Before sending assignments, make sure that your program passes the above input-output samples.