Question
The flowchart below represents an algorithm that allows a user to enter the number of sides, length of side and apothem (see diagram below) for a regular polygon. It then calculates and outputs the area and perimeter of this shape.
Worked example: a regular hexagon
Number of sides $=6$
Length of side $=10 \mathrm{~cm}$
Apothem $=8.66 \mathrm{~cm}$
Perimeter $=$ Number of sides $\times$ Length of side
$=6 \times 10$
$=60 \mathrm{~cm}$
Area $=\frac{\text { Perimeter } \times \text { Apothem }}{2}$
$=\frac{60 \times 8.66}{2}$
$=259.8 \mathrm{~cm}^2$
From the flowchart, pseudocode is to be created that will include sub‑programs to calculate and return the perimeter and area of the shape.
a.i.Construct pseudocode for the body of the following sub-program to calculate and return the perimeter of the shape.
SUB_PERIMETER(NUM_SIDES, LENGTH_SIDE)
// Pseudocode to be added
end SUB_PERIMETER[2]
a.ii.Construct a similar sub-program to the one in part (i) to calculate and return the area of the shape.
You must use an appropriate name for the sub-program and appropriate names for the parameters.[4]
a.iii.Construct pseudocode based on the flowchart to collect input from the user, call the sub-programs created in parts (i) and (ii), and output the results.[5]
b.)
Without using pseudocode, explain how your algorithms could be altered to also find the area and circumference of a circle.
Note:
Area of circle $=\pi r^2$
Circumference of circle $=2 \pi \mathrm{r}$
▶️Answer/Explanation
Ans:
a.i.)
Award [1] for a correct formula that uses the passed arguments;
Award [1] for a return statement;
Example 1:
SUB_PERIMETER (NUM_SIDES, LENGTH_SIDE)
//Pseudocode to be added
return NUM_SIDES * LENGTH_SIDE
End SUB_PERIMETER
a.ii)
Award [1] for all three correct inputs;
Award [1] for all three inputs with appropriate prompts;
Award [1] for the correct definition of PERIM;
Award [1] for the correct call of perimeter sub-program, including variables/arguments passed;
Award [1] for the correct call of area sub-program, including variables/arguments passed;
Award [1] for the return/output of both values;
Example 1:
//Main Program
output “Number of sides?”
input NUM_SIDES
output “Length of each side?”
input LENGTH_SIDE
output “Length of apothem?”
input APOTHEM
PERIM = SUB_PERIMETER(NUM_SIDES, LENGTH_SIDE)
output “Perimeter is “, PERIM
output “Area is “, SUB_AREA(PERIM, APOTHEM)
b.)
Award [1] for determining whether the shape is a polygon or a circle
Award [1] for inputting the radius (if a circle);
Award [1] for the creation of new subprogram (for area) OR
changing values of input parameters for the existing one (SUB_AREA ) OR
the calculation using correct formula (as given in the question paper PI * RADIUS * RADIUS)
Award [1] for the creation of a new subprogram (for circumference) OR
appropriately changing values of input parameters to the existing one (SUB_PERIMETER ) OR
the calculation using formula (as given in the question paper 2*PI * RADIUS)
Award [1] for calling both subprograms / outputting both values
Award [1] if after the alteration of the algorithm, it should still be able to calculate the area and the perimeter of the polygon as well as the area and the circumference of the circle
Example answer 1 (with existing subprograms, after inputting the three values: NUM_SIDES, LENGTH_SIDE, APOTHEM):
check if the inputted value for the NUM_SIDES is 1 or 0 (then the shape is a circle);
then inputted value for LENGTH_SIDE is to be used as radius;
2*3.14 should be set as NUM_SIDES and LENGTH_SIDE as APOTHEM;
call already written sub-programs(SUB_AREA and SUB_PERIMETER) so that algorithm calculates/finds correct area of circle and circumference;
if the shape is not a circle then code (as in Part(a)) for the polygon ( no other changes needed);
Example answer 2 (with new subprograms for area and circumference created):
construct two new sub-programs (for example, getCircleArea and getCircumference) ;
(in the main program) ask the user if the shape is a polygon or a circle;
if it is a polygon then code is as written in part a (no changes);
if it is a circle then ask the user to input the radius;
call both new subprograms to output area and circumference/ call the getCircleArea(radius) to find area and getCircumference(radius) sub-program to find circumference;
Example answer 3 (with simple if statement and formulas given in the question paper):
ask user to input if the shape is circle and use if statement to check this input value;
if not circle then code for the polygon (as written in part a);
otherwise (it is circle!) input radius;
calculate area;
and circumference using formulas given in the question;
Note to examiners: there may be other examples of correct answers.