Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 1 of 4.
Because ErrCode is in ascending order, stop as soon as a value is the same or bigger than the error number. Also stop at element 500.
PROCEDURE OutputError(LineNum, ErrNum : INTEGER)
DECLARE Index : INTEGER
Index ← 0
REPEAT
Index ← Index + 1
UNTIL ErrCode[Index] >= ErrNum OR Index = 500
IF ErrCode[Index] = ErrNum THEN
OUTPUT ErrText[Index], " on line ", LineNum
ELSE
OUTPUT "Unknown error on line ", LineNum
ENDIF
ENDPROCEDUREProcedure heading and ending with both parameters [1]
Conditional loop that adds 1 to Index each time [1]
...stops when the error number is found [1]
...or stops when ErrCode[Index] is greater than ErrNum (not present) [1]
...or after element 500 has been tested [1]
Tests whether it was found and outputs the description and line number, otherwise outputs the unknown error message with the line number [1]
Use a bubble sort with a no-swap flag and a shrinking boundary. Whenever ErrCode elements are swapped, swap the matching ErrText elements too.
PROCEDURE SortArrays()
DECLARE TempInt, J, Boundary : INTEGER
DECLARE TempStr : STRING
DECLARE NoSwaps : BOOLEAN
Boundary ← 499
REPEAT
NoSwaps ← TRUE
FOR J ← 1 TO Boundary
IF ErrCode[J] > ErrCode[J + 1] THEN
TempInt ← ErrCode[J]
ErrCode[J] ← ErrCode[J + 1]
ErrCode[J + 1] ← TempInt
TempStr ← ErrText[J]
ErrText[J] ← ErrText[J + 1]
ErrText[J + 1] ← TempStr
NoSwaps ← FALSE
ENDIF
NEXT J
Boundary ← Boundary - 1
UNTIL NoSwaps = TRUE
ENDPROCEDUREProcedure heading and ending [1]
Conditional outer loop that stops correctly [1]
An inner loop [1]
Correct range for the inner loop [1]
Compares element J with element J + 1, inside the loop [1]
Swaps the elements in both arrays, inside the loop [1]
No-swap flag: reset in the outer loop and set in the inner loop when a swap happens [1]
Another efficiency step, for example reducing the boundary each pass (or stopping the inner loop at the value 999) [1]
ErrCode is declared as a STRING but it should be an INTEGER [1]
Write any two:
The array of records can hold different data types under one identifier [1]
ErrCode and ErrText are closely linked, so they cannot get out of step as two arrays can; the code is simpler and easier to write, test and maintain [1]
Declare an array of 500 records:
DECLARE Error : ARRAY[1:500] OF ErrorRec
Correct array declaration with bounds 1:500 and type ErrorRec [1]
▶ Whole paper
Video solution of the whole paper · by Audere Prep. Tap the picture to open it on YouTube.
Solutions follow the official Cambridge mark schemes; marks are shown in grey. Videos are from other YouTube channels. Paper codes read component / series / year: M/J = May/June, O/N = October/November. Past-paper questions © UCLES, reproduced for classroom practice.