Computational Thinking and Data Structures: Question 3

Syllabus 9.2

Structured AS 8 marks

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:

  1. Set TotalCharge to 0.
  2. Input the number of orders placed today, NumOrders.
  3. Repeat the following steps NumOrders times: a. Input the weight, in kg, of the next order, OrderWeight. b. If OrderWeight is greater than 50, set Charge to 45.00. c. Otherwise, if OrderWeight is greater than 20, set Charge to 25.00. d. Otherwise, set Charge to 10.00. e. Add Charge to TotalCharge.
  4. 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:

IdentifierData typeDescription
NumOrdersINTEGERnumber of orders placed today
OrderWeightREALweight, in kg, of the order currently being read
ChargeREALdelivery charge worked out for the current order
TotalChargeREALrunning 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 NumOrdersNEXT 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, a FOR loop reading each OrderWeight, a nested IF/ELSE setting Charge, accumulating into TotalCharge, then OUTPUT TotalCharge
  • (c) Iteration, step 3 repeats steps 3a–3e a fixed number of times (NumOrders times)