9618/22/O/N/24 question 8: worked solution (October/November 2024, paper 22)
Show as plain textHide 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. Q62 9618/22/O/N/24 original Q8 Structured · 18 marks Worked solution (a) Search the array until an unassigned character with the right role is found or all 45 have been checked. When found, assign the player and stop the search. Then output the right message. PROCEDURE Assign(ThisPlayer : INTEGER, ThisRole : STRING) DECLARE Index : INTEGER DECLARE Done : BOOLEAN Done ← FALSE Index ← 1 WHILE Index <= 45 AND Done = FALSE IF Character[Index].Player = 0 AND Character[Index].Role = ThisRole THEN Character[Index].Player ← ThisPlayer Done ← TRUE ELSE Index ← Index + 1 ENDIF ENDWHILE IF Done = TRUE THEN OUTPUT Character[Index].Name, " the ", Character[Index].Role, " has been assigned to player ", ThisPlayer ELSE OUTPUT "No characters with this role are available" ENDIF ENDPROCEDURE Loop that ends when a character is found or all 45 elements have been checked [1] Test that the Player field is 0 (unassigned) inside the loop [1] ...AND the Role field matches ThisRole inside the loop [1] If an available character is found, ThisPlayer is assigned to its Player field [1] When found, a flag is set or the loop is ended [1] Both OUTPUT messages placed logically (found and not found) [1] Both OUTPUT statements correctly formed [1] (b) Open the file for writing, then for each of the 45 records join the four fields with '^' between them (turn the numbers into strings first) and write the line. PROCEDURE Save() DECLARE Index : INTEGER DECLARE Line : STRING OPENFILE "SaveFile.txt" FOR WRITE FOR Index ← 1 TO 45 Line ← NUM_TO_STR(Character[Index].Player) & "^" & Character[Index].Role & "^" & Character[Index].Name & "^" & NUM_TO_STR(Character[Index].Level) WRITEFILE "SaveFile.txt", Line NEXT Index CLOSEFILE "SaveFile.txt" ENDPROCEDURE Local integer Index and string Line declared [1] File SaveFile.txt opened in write mode and closed afterwards [1] Loop through all 45 elements [1] A line formed from all four fields inside the loop [1] NUM_TO_STR() used on both Player and Level [1] Three '^' separators correctly placed between the fields [1] The line is written to the file inside the loop [1] (c) Text files hold only text, so the Boolean must be turned into text. Encode Status as a character or string, for example "T" or "F" (or "TRUE"/"FALSE") [1] Add the '^' separator and then this character/string to the end of the line [1] (d) Make the filename different each time. Method: add a number (suffix) to the filename that goes up by one for each save [1] Example: SaveFile01.txt then SaveFile02.txt [1] KEY IDEA Search with a loop that stops on a flag, and save records by joining string fields with a separator on one line per record. TOTAL 18 marks Official mark scheme VIDEO ▶ Whole paper Video solution of the whole paper · by Papersdock. Tap the picture to open it on YouTube.