9618/22/O/N/24 question 6: worked solution (October/November 2024, paper 22)

Show as plain text

Cambridge AS Level Computer Science 9618 · 10.2 Arrays Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 2 of 4. Q33 9618/22/O/N/24 original Q6 Structured · 9 marks 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 IDEA Keep 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 VIDEO ▶ Whole paper Video solution of the whole paper · by Papersdock. Tap the picture to open it on YouTube.