9618/22/M/J/25 question 5: worked solution (May/June 2025, paper 22)
Show as plain textHide plain text
Cambridge AS Level Computer Science 9618 · 12.2 Program Design Worked solution from the Software Development Paper 2 Problem-solving and Programming booklet, Part 2 of 3. Q28 9618/22/M/J/25 original Q5 Structured · 12 marks 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: From Label To standby mode turn on detect mode detect mode turn off standby mode detect mode movement detected active mode active mode 20 seconds elapsed sequence complete active mode turn off standby mode sequence complete time saved detect 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 IDEA A 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 VIDEO ▶ Whole paper Video solution of the whole paper · by Awab Aqib. Tap the picture to open it on YouTube.