Cambridge AS Level Computer Science 9618 · 10.1 Data Types and Records

Q3 · 9618/22/O/N/22 Q7

Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 1 of 4.

Q39618/22/O/N/22original Q7Structured · 18 marks
Question 7 from 9618/22/O/N/22

Worked solution

(a)

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
ENDPROCEDURE

Procedure 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]

(b)

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
ENDPROCEDURE

Procedure 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]

(c)(i)

ErrCode is declared as a STRING but it should be an INTEGER [1]

(c)(ii)

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]

(c)(iii)

Declare an array of 500 records:

DECLARE Error : ARRAY[1:500] OF ErrorRec

Correct array declaration with bounds 1:500 and type ErrorRec [1]

KEY IDEASorted data lets a search stop early, and a bubble sort can be made efficient with a flag and a shrinking boundary.
TOTAL 18 marks

Official mark scheme

Mark scheme for 9618/22/O/N/22 question 7
VIDEO
▶ 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.