Programming Constructs and Operators: Question 2

Syllabus 8.1

Structured 6 marks

A mobile network calculates the extra cost of a customer's data top-up using this pseudocode. Data is charged in complete blocks of 500 MB, worked out using the DIV operator, and a CASE OF statement then selects the cost band for that number of blocks.

DECLARE DataUsedMB : INTEGER
DECLARE Blocks : INTEGER
DECLARE Cost : REAL
DataUsedMB ← 1350
Blocks ← DataUsedMB DIV 500
CASE OF Blocks
    0         : Cost ← 0.00
    1         : Cost ← 3.00
    2         : Cost ← 5.50
    OTHERWISE : Cost ← 5.50 + (Blocks - 2) * 2.00
ENDCASE
OUTPUT Cost

(a) State the value of Blocks after this algorithm executes, showing your working. [2]

(b) State the value output for Cost, and identify which branch of the CASE OF statement is used to produce it. [2]

(c) A second customer uses 2870 MB of data in the same month. State the value output for Cost for this customer, showing your working. [2]

Show worked solution Hide worked solution

Worked solution

Part (a): Finding Blocks with DIV

Blocks ← DataUsedMB DIV 500, with DataUsedMB = 1350.

DIV finds the largest whole number of 500s that fit into 1350, discarding any remainder:

  • 500 × 2 = 1000 (fits, since 1000 is less than or equal to 1350)
  • 500 × 3 = 1500 (too big, since 1500 is greater than 1350)

So 1350 DIV 500 = 2. [2 marks]: [1] for identifying that 2 whole blocks of 500 fit into 1350, [1] for the correct final value Blocks = 2.

Part (b): Selecting the CASE OF branch

The CASE OF Blocks statement checks the value of Blocks (2) against each listed value in turn:

  • Blocks = 0? No.
  • Blocks = 1? No.
  • Blocks = 2? Yes. This branch is used, so Cost ← 5.50.

Because a match was found at Blocks = 2, the OTHERWISE branch is never reached for this customer. [2 marks]: [1] for the branch identified (Blocks = 2), [1] for the correct output Cost = 5.50.

Part (c): Tracing a second customer with DataUsedMB = 2870

First, Blocks must be recalculated for the new input. It is not carried over from part (a):

Blocks ← 2870 DIV 500

  • 500 × 5 = 2500 (fits, since 2500 is less than or equal to 2870)
  • 500 × 6 = 3000 (too big, since 3000 is greater than 2870)

So Blocks = 5. [1 mark]

Since 5 does not match any of the listed values 0, 1 or 2, the OTHERWISE branch runs:

Cost ← 5.50 + (Blocks - 2) * 2.00 = 5.50 + (5 - 2) * 2.00 = 5.50 + (3 * 2.00) = 5.50 + 6.00 = 11.50

So Cost = 11.50. [1 mark] for correctly applying the OTHERWISE formula (subtracting 2 from Blocks before multiplying by 2.00) and reaching this value.

Final answers

  • (a) Blocks = 2
  • (b) Cost = 5.50, from the Blocks = 2 branch
  • (c) Cost = 11.50, from the OTHERWISE branch (Blocks = 5)