Cambridge AS Level Computer Science 9618 · 10.2 Arrays

Q24 · 9618/21/O/N/23 Q4

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

Q249618/21/O/N/23original Q4Structured · 9 marks
Question 4 from 9618/21/O/N/23

Worked solution

(a)

Write a function with two parameters, count the matching strings in a loop, then compare the count with the maximum after the loop.

FUNCTION TooMany(Search : STRING, Max : INTEGER) RETURNS BOOLEAN DECLARE Count, Index : INTEGER Count ← 0 FOR Index ← 1 TO 150 IF Data[Index] = Search THEN Count ← Count + 1 ENDIF NEXT Index IF Count > Max THEN RETURN TRUE ELSE RETURN FALSE ENDIF ENDFUNCTION

Function heading, ending and RETURNS BOOLEAN [1]

Count and Index declared as integers [1]

Count set to 0 before the loop [1]

Loop (any type) that runs 150 times [1]

Each element of Data compared with the search string, and Count increased if equal, inside the loop [1]

Count compared with Max after the loop, returning TRUE or FALSE in both cases [1]

(b)

Check the row is even with MOD, and check both columns of that row. Both conditions go in one IF with AND.

IF Row MOD 2 = 0 AND (Data[Row, 1] = Search OR Data[Row, 2] = Search) THEN

Test that the row is an even number, for example Row MOD 2 = 0 [1]

Test whether either column equals the search string, using OR [1]

One correctly formed selection: a single IF with AND, or two nested IFs, or a loop using STEP 2 from row 2 [1]

KEY IDEACount matches in a loop and make the final comparison and return only after the loop has finished.
TOTAL 9 marks

Official mark scheme

Mark scheme for 9618/21/O/N/23 question 4
VIDEO
▶ Starts at Q4 (19:01)

Starts at Q4 (19:01) · by Pseudocode Pro. 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.