Cambridge AS Level Computer Science 9618 · 9.2 Algorithms

Q44 · 9618/21/O/N/25 Q2

Worked solution from the Algorithm Design and Problem-solving Paper 2 Problem-solving and Programming booklet, Part 2 of 2.

Q449618/21/O/N/25original Q2Structured · 5 marks
Question 2 from 9618/21/O/N/25

Worked solution

Draw a loop that checks each of the 30 elements, with a counter for the number output:

START → Set Index to 1 and Count to 0 → decision "Is Index = 31?"

If NO: decision "Is Data[Index] = ""?" If YES (blank): Set Index to Index + 1 and go back. If NO: Set Count to Count + 1 → Output Data[Index] → Set Index to Index + 1 → back to the first decision.

If YES: Output Count → END.

Index and Count both set before the loop [1]

Loop that goes through exactly 30 elements [1]

Test for an empty string (or a non-empty one) in the current element [1]

Count increased when the element is not blank, inside the loop [1]

Both outputs (each non-blank element in the loop, and Count after it) under the correct conditions [1]

Testing for the empty string first is also accepted.

KEY IDEAInitialise the index and count, loop through the array, test each element, count and output the non-blank ones, then output the count.
TOTAL 5 marks

Official mark scheme

Mark scheme for 9618/21/O/N/25 question 2
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.