Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 2 of 4.
Use an outer loop for the 128 columns and an inner loop for the 6 users. Add up only the values above 10, count them, then divide.
PROCEDURE Mix()
DECLARE Count, Total, ThisUser, ThisSample : INTEGER
FOR ThisSample ← 1 TO 128
Count ← 0
Total ← 0
FOR ThisUser ← 1 TO 6
IF Sample[ThisUser, ThisSample] > 10 THEN
Count ← Count + 1
Total ← Total + Sample[ThisUser, ThisSample]
ENDIF
NEXT ThisUser
Result[ThisSample] ← INT(Total / Count)
NEXT ThisSample
ENDPROCEDURECount and Total declared and set to 0 before the inner loop [1]
Outer loop that runs 128 times [1]
Inner loop that runs 6 times [1]
Test for a sample greater than 10 inside the loop [1]
If true, add to Total and increase Count [1]
Average calculated after the inner loop (inside the outer loop) and stored in Result, converted to an integer with INT() or DIV [1]
Other correct ways also score.
▶ Starts at Q6 (43:33)
Starts at Q6 (43:33) · 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.