Cambridge AS Level Computer Science 9618 · 10.2 Arrays

Q38 · 9618/21/O/N/25 Q4

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

Q389618/21/O/N/25original Q4Structured · 9 marks
Question 4 from 9618/21/O/N/25

Worked solution

(a)

Input the first value and store it. Then keep inputting while the array is not full and each value is bigger than the one before.

PROCEDURE Store()
    DECLARE ThisNum, LastNum : REAL
    DECLARE Index : INTEGER
    INPUT ThisNum
    LastNum ← ThisNum
    Number[1] ← ThisNum
    INPUT ThisNum
    Index ← 2
    WHILE Index < 21 AND ThisNum > LastNum
        Number[Index] ← ThisNum
        LastNum ← ThisNum
        Index ← Index + 1
        IF Index < 21 THEN
            INPUT ThisNum
        ENDIF
    ENDWHILE
    OUTPUT Index - 1, " values were stored in the array"
ENDPROCEDURE

Local variables declared: Index as INTEGER, ThisNum and LastNum as REAL [1]

First value input and assigned to the first element [1]

Attempt at a conditional loop with both tests (array not full, value larger than previous) [1]

Completely correct conditional loop with both tests [1]

Inside the loop: value assigned to the current element when larger, and the next value input [1]

After the loop: output of the count of values stored with a message [1]

A count-controlled FOR loop with a BREAK (or similar exit) also scores.

(b)(i)

Any two of these:

The number of values is not fixed, so it is not limited to the size of the array [1]

The data is stored permanently (non-volatile), so another program can use it without the values being typed in again [1]

(b)(ii)

Each real value must be converted to a string before it is written. [1]

KEY IDEAAn array has a fixed size and loses its data when the program ends; a text file can grow and keeps its data, but holds strings.
TOTAL 9 marks

Official mark scheme

Mark scheme for 9618/21/O/N/25 question 4
VIDEO
▶ Whole paper

Video solution of the whole paper · by CSFeedbySirNisarAli. 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.