9618/23/O/N/24 question 8: worked solution (October/November 2024, paper 23)

Show as plain text

Cambridge AS Level Computer Science 9618 · 10.3 Files Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 3 of 4. Q63 9618/23/O/N/24 original Q8 Structured · 16 marks Worked solution (a) 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 ENDPROCEDURE Num 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] (b) 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" ENDPROCEDURE File 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] (c) 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] KEY IDEA Loop through records counting and summing matches, and use a helper function to split saved lines back into fields. TOTAL 16 marks Official mark scheme VIDEO ▶ Whole paper Video solution of the whole paper · by Zafar Ali Khan (ZAK). Tap the picture to open it on YouTube.