Computational Thinking and Data Structures: Question 3
Syllabus 9.2
A furniture store calculates a delivery charge for each order based on its weight, then adds up the charges for all of that day's orders. The task is described using this structured English description:
- Set
TotalChargeto 0. - Input the number of orders placed today,
NumOrders. - Repeat the following steps
NumOrderstimes: a. Input the weight, in kg, of the next order,OrderWeight. b. IfOrderWeightis greater than 50, setChargeto 45.00. c. Otherwise, ifOrderWeightis greater than 20, setChargeto 25.00. d. Otherwise, setChargeto 10.00. e. AddChargetoTotalCharge. - Output
TotalCharge.
(a) Complete an identifier table for this algorithm, giving a suitable Cambridge pseudocode
data type for each of the four identifiers NumOrders, OrderWeight, Charge and
TotalCharge. [2]
(b) Write Cambridge pseudocode for the complete algorithm described above, using a count-controlled loop and selection statement(s) as needed. [5]
(c) Step 3 of the structured English description is an example of one of the three basic constructs used in pseudocode: sequence, selection or iteration. State which construct this is, and justify your answer. [1]
Show worked solution Hide worked solution
Worked solution
Part (a): Identifier table
Each identifier is chosen a data type that matches the kind of value it will hold. Order weights and money values in this problem are not restricted to whole numbers, so they need a data type that can store decimals:
| Identifier | Data type | Description |
|---|---|---|
NumOrders | INTEGER | number of orders placed today |
OrderWeight | REAL | weight, in kg, of the order currently being read |
Charge | REAL | delivery charge worked out for the current order |
TotalCharge | REAL | running total of all of today’s delivery charges |
[2 marks]: [1] for NumOrders : INTEGER, [1] for OrderWeight, Charge and
TotalCharge all given REAL.
Part (b): Pseudocode for the algorithm
NumOrders is known before the loop starts and does not change while the loop runs, so a
count-controlled FOR loop is the appropriate construct for step 3. Within the loop, the
weight bands in steps 3b–3d are naturally expressed as a nested IF / ELSE:
DECLARE NumOrders : INTEGER
DECLARE OrderWeight : REAL
DECLARE Charge : REAL
DECLARE TotalCharge : REAL
DECLARE OrderNumber : INTEGER
TotalCharge ← 0
INPUT NumOrders
FOR OrderNumber ← 1 TO NumOrders
INPUT OrderWeight
IF OrderWeight > 50
THEN
Charge ← 45.00
ELSE
IF OrderWeight > 20
THEN
Charge ← 25.00
ELSE
Charge ← 10.00
ENDIF
ENDIF
TotalCharge ← TotalCharge + Charge
NEXT OrderNumber
OUTPUT TotalCharge
[5 marks]: [1] for TotalCharge ← 0 before the loop and INPUT NumOrders, [1] for
a FOR OrderNumber ← 1 TO NumOrders… NEXT OrderNumber loop with the correct bound, [1]
for INPUT OrderWeight inside the loop, [1] for the nested IF/ELSE correctly assigning
45.00, 25.00 or 10.00 to Charge based on the two conditions in the right order, [1]
for TotalCharge ← TotalCharge + Charge inside the loop and OUTPUT TotalCharge after it ends.
Part (c): Identifying the construct in step 3
Step 3 reads “repeat the following steps NumOrders times”, which causes the block of steps
3a–3e to be carried out over and over, a fixed number of times, rather than being carried out
just once (sequence) or triggering a choice between two or more alternative branches
(selection). This repeated execution of the same steps is the defining feature of
iteration. [1 mark]
Final answers
- (a)
NumOrders : INTEGER,OrderWeight : REAL,Charge : REAL,TotalCharge : REAL - (b) See the pseudocode above:
TotalCharge ← 0,INPUT NumOrders, aFORloop reading eachOrderWeight, a nestedIF/ELSEsettingCharge, accumulating intoTotalCharge, thenOUTPUT TotalCharge - (c) Iteration, step 3 repeats steps 3a–3e a fixed number of times (
NumOrderstimes)