Cambridge AS Level Computer Science 9618 · 12.2 Program Design

Q28 · 9618/22/M/J/25 Q5

Worked solution from the Software Development Paper 2 Problem-solving and Programming booklet, Part 2 of 3.

Q289618/22/M/J/25original Q5Structured · 12 marks
Question 5 from 9618/22/M/J/25

Worked solution

(a)

Draw the three new states, then one arrow for each row of the table. Arrows must point from the current state to the next state.

Draw and label the three new states: detect mode, active mode and sequence complete [1]

Two events drawn with labels, joining the correct states with the arrow the right way round [1]

Four events drawn correctly in the same way [1]

All six events drawn correctly, and no extra ones [1]

The six arrows are:

FromLabelTo
standby modeturn ondetect mode
detect modeturn offstandby mode
detect modemovement detectedactive mode
active mode20 seconds elapsedsequence complete
active modeturn offstandby mode
sequence completetime saveddetect mode

(b)

Read the first line to get its hour and start a count. For each next line, compare the hour with the last hour. If the same, add 1 to the count. If different, output the old hour and count, then start again. Output the last hour after the loop.

DECLARE Count : INTEGER
DECLARE Line : STRING
DECLARE NextHour, Hour : STRING
OPENFILE "TimeTaken.txt" FOR READ
Count ← 1
READFILE "TimeTaken.txt", Line
Hour ← LEFT(Line, 2)
WHILE NOT EOF("TimeTaken.txt")
    READFILE "TimeTaken.txt", Line
    NextHour ← LEFT(Line, 2)
    IF NextHour = Hour THEN
        Count ← Count + 1
    ELSE
        OUTPUT "Hour : ", Hour, " Total : ", Count
        Hour ← NextHour
        Count ← 1
    ENDIF
ENDWHILE
OUTPUT "Hour : ", Hour, " Total : ", Count
CLOSEFILE "TimeTaken.txt"

Count set to 1 (or to 0 before the first line is counted) [1]

File opened for read and closed after use [1]

Loop that repeats until the end of the file [1]

A line is read from the file inside the loop [1]

The hour is taken from the line (for example LEFT(Line, 2)) inside the loop [1]

The current hour is compared with the last hour read [1]

If the hours are the same, the count goes up by 1 [1]

If different, the hour and count are output with a message, Hour is updated and Count is set to 1 [1]

The final hour and count are output once, after the loop [1] (only 8 marks can be earned in all)

Another correct way also scores: an array of 24 counts, one for each hour, set to 0 first. Convert the hour with STR_TO_NUM, add 1 to that element, then output each hour whose count is not 0.

KEY IDEAA state-transition diagram has one labelled arrow per row of the table, and a control-break loop outputs a total each time the hour changes.
TOTAL 12 marks

Official mark scheme

Mark scheme for 9618/22/M/J/25 question 5
VIDEO
▶ Whole paper

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