Cambridge AS Level Computer Science 9618 · 10.2 Arrays

Q33 · 9618/22/O/N/24 Q6

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

Q339618/22/O/N/24original Q6Structured · 9 marks
Question 6 from 9618/22/O/N/24

Worked solution

(a)

Pick a random index and repeat until the element is not an empty string. Do this for the first filling, then for the second filling (which must also differ from the first), then for the bread. Use INT(RAND(n)) + 1 to get an index from 1 to n.

PROCEDURE Special()
    DECLARE Index : INTEGER
    DECLARE Filling1, Filling2 : STRING
    REPEAT
        Index ← INT(RAND(35)) + 1
    UNTIL Filling[Index] <> ""
    Filling1 ← Filling[Index]
    REPEAT
        Index ← INT(RAND(35)) + 1
    UNTIL Filling[Index] <> "" AND Filling[Index] <> Filling1
    Filling2 ← Filling[Index]
    REPEAT
        Index ← INT(RAND(10)) + 1
    UNTIL Bread[Index] <> ""
    OUTPUT "The daily special is ", Filling1, " and ", Filling2, " on ", Bread[Index], " bread."
ENDPROCEDURE

Loop for the first filling that avoids unused elements [1]

Loop for the second filling that avoids unused elements [1]

Check that the second filling is different from the first (compare contents or indices) [1]

Loop for the bread that avoids unused elements [1]

Uses RAND(35) for fillings and RAND(10) for bread [1]

RAND used completely correctly everywhere, with INT() and + 1 [1]

Correct output, once only, after the fillings and bread have been chosen [1]

(b)

Store the rules that stop two fillings going together.

For each filling, keep a list of incompatible fillings (or indices) [1]

When choosing the second filling, also check that it is not on the first filling's list [1]

Another way that scores: keep a list of good combinations and pick one at random (this gets both marks).

KEY IDEAKeep picking random positions until you land on a used element, and use extra checks (or a list of allowed pairs) to control combinations.
TOTAL 9 marks

Official mark scheme

Mark scheme for 9618/22/O/N/24 question 6
VIDEO
▶ Whole paper

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