Cambridge AS Level Computer Science 9618 · 10.2 Arrays

Q21 · 9618/23/O/N/22 Q7

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

Q219618/23/O/N/22original Q7Structured · 17 marks
Question 7 from 9618/23/O/N/22

Worked solution

(a)

Declare the variables, ask for the two error numbers, then step through ErrCode. Stop when an error number is bigger than the last number or the end of the array is reached. Count and output each error number in range.

PROCEDURE OutputRange()
    DECLARE First, Last, Count, Index, ThisErr : INTEGER
    DECLARE ThisMess : STRING
    DECLARE PastLast : BOOLEAN
    Count ← 0
    Index ← 1
    PastLast ← FALSE
    OUTPUT "Please input first error number: "
    INPUT First
    OUTPUT "Please input last error number: "
    INPUT Last
    OUTPUT "List of error numbers from ", First, " to ", Last
    WHILE Index < 501 AND NOT PastLast
        ThisErr ← ErrCode[Index]
        IF ThisErr > Last THEN
            PastLast ← TRUE
        ELSE
            IF ThisErr >= First THEN
                ThisMess ← ErrText[Index]
                IF ThisMess = "" THEN
                    ThisMess ← "Error Text Missing"
                ENDIF
                OUTPUT ThisErr, " : ", ThisMess
                Count ← Count + 1
            ENDIF
        ENDIF
        Index ← Index + 1
    ENDWHILE
    OUTPUT Count, " error numbers output"
ENDPROCEDURE

Local integer Count declared and set to 0 [1]

Suitable prompt and two inputs [1]

Conditional loop that stops when the error number is past the range (or 999 is reached) [1]

...and also stops at the end of the array [1]

Check inside the loop whether this ErrCode needs to be output [1]

Check for blank error text inside the loop [1]

Output in both cases (with the warning text when blank) and increase Count inside the loop [1]

Output of a header before the loop and the summary with Count after the loop [1]

(b)(i)

Look for the first unused element (999), store the new values there, sort, then work out how many unused elements are left.

FUNCTION AddError(ErrNum : INTEGER, ErrMess : STRING) RETURNS INTEGER
    DECLARE Index, Remaining : INTEGER
    CONSTANT Unused = 999
    Index ← 1
    Remaining ← -1
    REPEAT
        IF ErrCode[Index] = Unused THEN
            ErrCode[Index] ← ErrNum
            ErrText[Index] ← ErrMess
            CALL SortArrays()
            Remaining ← 500 - Index
        ENDIF
        Index ← Index + 1
    UNTIL Remaining <> -1 OR Index > 500
    RETURN Remaining
ENDFUNCTION

Conditional loop that ends when the item is added or the end of the array is reached [1]

Test for an unused element (999) inside the loop [1]

Assign the error number and description to the arrays (or save the index and assign after the loop) [1]

Loop is ended once the empty element is found [1]

Call SortArrays() once [1]

Calculate the number of unused elements and return an integer in both cases (-1 if not added) [1]

(b)(ii)

Search for the error number, mark its place as unused, then re-sort.

Loop through the 500 elements (while the error number has not been found) [1]

Compare ErrCode for the current element with the error number to remove [1]

If they are the same, set that element to 999 and stop the loop [1]

Also credited: call SortArrays() once afterwards to move the 999 to the end (only three marks in total are available).

KEY IDEASearch the array with a loop that stops at the right place, deal with every case, and keep the ascending order by using SortArrays().
TOTAL 17 marks

Official mark scheme

Mark scheme for 9618/23/O/N/22 question 7
VIDEO
▶ Starts at Q7 (19:02)

Starts at Q7 (19:02) · by Zafar Ali Khan (ZAK). 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.