We want to be able to specify the size of the figure. The size of the figure given above is 5. It means that the first
row of the figure consists of 5 ^ characters followed by one # followed by 5 ^ characters. There is one row more
than the specified size (i.e. 6 rows in this case).
We give the function drawShape that displays such a figure on the screen. The function is a void function with
one parameter (of type int), namely the size as indicated above. The function uses a nested for loop. If the size of
the figure is represented by sizeP,then the i-th row starts with (sizeP - i + 1) ^ characters, followed by
(2 * i - 1) # characters, followed by another (sizeP - i + 1) ^ characters. There is(2 * sizeP +
1) # characters in the last row. Apart from illustrating the void function, we illustrate the powerful use of the for
loop in the function. You will not be required to write such a function in the exam.
Your task is to write the main function. Ask the user for the size of the figure. Validate that the size is at least 4 – if
the user enters a value less than 4, ask the user to input the size again. Then call function drawShape to display the
figure. Test your program with a size of 5. Make sure the figure is displayed correctly, but do not submit printouts of
the program or the output.
// Assignment 2 Question 3a
#include <iostream>
using namespace std;
void drawShape(int sizeP)
{
for (int i = 1; i <= sizeP; i++)
{
for (int j = 1; j <= (sizeP - i + 1) ; j++)
cout << '^';
for (int j = 1; j <= (2 * i - 1); j++)
cout << '#';
for (int j = 1; j <= (sizeP - i + 1) ; j++)
cout << '^';
cout << endl;
}
for (int i = 1; i <= (2 * sizeP + 1); i++) // bottom row
cout << '#';
cout << endl;
}
int main( )
{
.
.
.
}
Question 3b: Three value parameters
In Question 3a the # character formed the triangle in the figure. Change function drawShape in such a way that we
will be able to specify what character should form this triangle. Also allow the part of the figure currently displayed
by the ^ character, to be specified. Three values will therefore now be input, namely the size of the pattern, the
character to represent the triangle, and a character to represent the rest of the figure. You will have to change the
main function to ask the user to input these two characters, and change the calling statement to the function
drawShape to send the size of the figure and the two characters entered to the function. Use proper cout
statements so that it is clear to the user what the purpose of the characters are that has to be supplied as input.
Function drawShape now should have three parameters, one of type int (namely the size) and two of type char
(namely the two characters that has to form the pattern of the figure). Input 7 as the size, S as the character to display
the triangle and + as the character to form the rest of the figure, and submit printouts of the program and the output.
Hint: When you copy your output into a Word document, change the font of the figure to Courier New. Some of
the fonts display the figure slightly angled to the right.
// Assignment 2 Question 3b
#include <iostream>
using namespace std;
// The changed drawShape function should be inserted here.