Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 2 of 4.
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]
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]
▶ 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.