Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 3 of 4.
Count matching characters and add up their skill levels. Output after the loop.
PROCEDURE Count(ThisPlayer : INTEGER, ThisRole : STRING)
DECLARE Index, Num, Total : INTEGER
Num ← 0
Total ← 0
FOR Index ← 1 TO 45
IF Character[Index].Player = ThisPlayer AND Character[Index].Role = ThisRole THEN
Num ← Num + 1
Total ← Total + Character[Index].SkillLevel
ENDIF
NEXT Index
IF Num > 0 THEN
OUTPUT "Player ", ThisPlayer, " has ", Num, " characters with the role of ", ThisRole, " and the total skill level is ", Total
ELSE
OUTPUT "No characters with that role are assigned to this player"
ENDIF
ENDPROCEDURENum and Total declared as integers and set to 0 [1]
Loop through all 45 elements [1]
Player field compared with ThisPlayer inside the loop [1]
Role field compared with ThisRole inside the loop [1]
If both match, Num is increased by 1 and SkillLevel is added to Total [1]
After the loop, test whether any matches were found [1]
Both OUTPUT statements correctly formed, with only one output [1]
Open the file for reading. For each of the 45 lines, read it, then use Extract() for each field number. Change the number fields with STR_TO_NUM.
PROCEDURE Restore()
DECLARE Index : INTEGER
DECLARE Line : STRING
OPENFILE "SaveFile.txt" FOR READ
FOR Index ← 1 TO 45
READFILE "SaveFile.txt", Line
Character[Index].Player ← STR_TO_NUM(Extract(Line, 1))
Character[Index].Role ← Extract(Line, 2)
Character[Index].Name ← Extract(Line, 3)
Character[Index].SkillLevel ← STR_TO_NUM(Extract(Line, 4))
NEXT Index
CLOSEFILE "SaveFile.txt"
ENDPROCEDUREFile opened in read mode and closed afterwards [1]
Loop through 45 elements [1]
A line read from the file inside the loop [1]
Extract() used inside the loop [1]
Extract() used correctly for all four fields (Line and field number 1 to 4) [1]
STR_TO_NUM() used on Player and SkillLevel [1]
All fields correctly extracted and assigned to the right record [1]
Say what saving lets the player do.
The Character data can be saved before the program is closed [1]
The game can then continue later with the same data, from the point where it was saved [1]
▶ Whole paper
Video solution of the whole paper · by Zafar Ali Khan (ZAK). 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.