Past paper – A Level Computer Science (9618)

Cambridge International AS & A Level · 9618

Computer Science · Paper 42

Oct/Nov 2024 · 9618/42/O/N/24

Questions3
Marks75
TypeWritten

Answer on paper, open each mark scheme and give yourself marks, then open the Summary.

0 / 0Answer a question to start your score.
Saved 0
Advertisement
1
9618/42/O/N/24 · Question 1Oct/Nov 2024Paper 4229 marks
Loading question…
Question text

1
A computer game is designed for users to select characters. Each character can take part in a
group of events. Each group has five events. There are four types of event: jump, swim, run, drive.
The program is written using object-oriented programming.
(a) The class EventItem stores data about the events.
EventItem
EventName : STRING
stores the name of the event
Type : STRING
stores the type of event, either: jump, swim, run or drive
Difficulty : INTEGER
stores the difficulty of the event from 1 (easiest) to 5
(hardest)
Constructor()
initialises EventName, Type and Difficulty to its
parameter values
GetName()
returns the name of the event
GetDifficulty()
returns the difficulty of the event
GetEventType()
returns the type of event
Write program code to declare the class EventItem and its constructor.
(i)
Do not declare the other methods.
Use your programming language’s appropriate constructor.
All attributes must be private.
If you are writing in Python, include attribute declarations, using comments.
Save your program as Question1_N24.
Copy and paste the program code into part 1(a)(i) in the evidence document.
[4]
The get methods GetName(), GetDifficulty() and GetEventType() each return
(ii)
the relevant attribute.
Write program code for the three get methods.
Save your program.
Copy and paste the program code into part 1(a)(ii) in the evidence document.
[3]
(b) The array Group stores five objects of type EventItem.
Write program code to declare Group local to the main program.
(i)
Save your program.
Copy and paste the program code into part 1(b)(i) in the evidence document.
[1]
(ii)
One group has the following events:
Event name
Event type
Event difficulty
Bridge
jump
3
Water wade
swim
4
100 mile run
run
5
Gridlock
drive
2
Wall on wall
jump
4
Write program code to create an instance of EventItem for each of the five events, and
store them in Group.
Save your program.
Copy and paste the program code into part 1(b)(ii) in the evidence document.
[3]
(c) The class Character stores data about the characters in the game.
Each character has a skill level for each type of event. The skill level is an integer between
1 and 5 inclusive. Skill level 1 is the lowest skill level, and skill level 5 is the highest skill level.
Character
CharacterName : STRING
stores the name of the character
Jump : INTEGER
stores the character’s skill level at events of type jump
Swim : INTEGER
stores the character’s skill level at events of type swim
Run : INTEGER
stores the character’s skill level at events of type run
Drive : INTEGER
stores the character’s skill level at events of type drive
Constructor()
initialises CharacterName, Jump, Swim, Run and
Drive to its parameter values
GetName()
returns the name of the character
CalculateScore()
takes the type of event and difficulty as parameters.
Calculates and returns the chance of the character
completing the event
Write program code to declare the class Character, its constructor and get method.
Use your programming language’s appropriate constructor.
All attributes must be private.
If you are writing in Python, include attribute declarations, using comments.
Save your program.
Copy and paste the program code into part 1(c) in the evidence document.
[4]
(d) The method CalculateScore() in the Character class calculates and returns the
percentage chance of a character completing an event.
When a character’s skill level is greater than or equal to the difficulty of that event, the
percentage chance of completing the event is 100%.
When a character’s skill level is less than the difficulty of that event, the character’s skill level
is subtracted from the difficulty of that event. This difference is used to identify the percentage
chance of success using this table:
Percentage chance
Difference
of success
1
80
2
60
3
40
4
20
For example:
•
A character has a skill level of 3 for events of type run.
•
An event of type run has a difficulty level of 5
•
The

2
9618/42/O/N/24 · Question 2Oct/Nov 2024Paper 4229 marks
Loading question…
Question text

2
A linear queue data structure is designed using a record structure.
The record structure Queue has the following fields:
•
QueueArray, a 1D array of up to 100 integer values
•
Headpointer, a variable that stores the index of the first data item in QueueArray
•
Tailpointer, a variable that stores the index of the next free location in QueueArray.
(a) Write program code to declare the record structure Queue and its fields.
If your programming language does not support record structures, a class can be declared
instead.
If you are writing in Python, use comments to declare the appropriate data types.
Save your program as Question2_N24.
Copy and paste the program code into part 2(a) in the evidence document.
[3]
(b) The main program creates a new Queue record with the identifier TheQueue. The head
pointer is initialised to –1. The tail pointer is initialised to 0. Each element in the array is
initialised with –1.
Write program code for the main program.
Save your program.
Copy and paste the program code into part 2(b) in the evidence document.
[3]
(c) The pseudocode function Enqueue() inserts an integer value into the queue.
The function is incomplete. There are three incomplete statements.
FUNCTION Enqueue(BYREF AQueue : Queue, BYVAL TheData : INTEGER)
RETURNS INTEGER
IF AQueue.Headpointer = -1 THEN
← …
AQueue.QueueArray[AQueue.Tailpointer]
← 0
AQueue.Headpointer
AQueue.Tailpointer ← AQueue.Tailpointer + 1
RETURN 1
ELSE
IF AQueue.Tailpointer > … THEN
RETURN -1
ELSE
← TheData
AQueue.QueueArray[AQueue.Tailpointer]
← AQueue.Tailpointer …
AQueue.Tailpointer
RETURN 1
ENDIF
ENDIF
ENDFUNCTION
Write program code for Enqueue().
Save your program.
Copy and paste the program code into part 2(c) in the evidence document.
[5]
(d) The function ReturnAllData() accesses TheQueue. It concatenates all the integer
values that have been inserted into the queue’s array, starting from the value stored at
HeadPointer, with a space between each integer value. The string of concatenated values
is returned.
None of the integer values are removed from the queue.
Write program code for ReturnAllData().
Save your program.
Copy and paste the program code into part 2(d) in the evidence document.
[3]
(e) (i)
The main program asks the user to enter 10 integers with values of 0 or greater. It reads
each input repeatedly until a valid number is entered.
All 10 valid inputs are added to the queue, using Enqueue().
If the value returned from Enqueue() is –1, a message is output to state that the queue
is full, otherwise a message is output to state that the item has been added to the queue.
The function ReturnAllData() is called once all 10 integers have been entered and
the return value from the function call is output.
Amend the main program to perform these actions.
Save your program.
Copy and paste the program code into part 2(e)(i) in the evidence document.
[5]
(ii)
Test your program with the following inputs in the order given:
10
9
–1
8
7
6
5
4
3
2
1
Take a screenshot of the output.
Save your program.
Copy and paste the screenshot into part 2(e)(ii) in the evidence document.
[2]
The function Dequeue() accesses TheQueue. The function returns –1 if the queue is empty.
(f)
If the queue is not empty, the function returns the next item in the queue and updates the
relevant pointer(s).
The data is not replaced or deleted from the queue.
Write program code for Dequeue().
Save your program.
Copy and paste the program code into part 2(f) in the evidence document.
[4]
The main program calls Dequeue() twice, and each time it either outputs ‘Queue empty’
(g) (i)
if there is no data in the queue or outputs the return value.
The main program then calls ReturnAllData() a second time.
Amend the main program.
Save your program.
Copy and paste the program code into part 2(g)(i) in the evidence document.
[3]
9618/42/O/N/

3
9618/42/O/N/24 · Question 3Oct/Nov 2024Paper 4217 marks
Loading question…
Question text

The text file HighScoreTable.txt stores the top seven player scores for a game.
3
The data in the file is stored in the order:
Player ID
Game level
Score
Each item of data is stored on a new line. For example, the first set of data in the file is:
Player ID:
GHEH
Game level:
3
Score:
10
(a) The data about the players and their scores is stored in a 2D array of strings with the identifier
HighScores.
The first dimension of the array has seven elements: one for each player. The second
dimension of the array has three elements: one for the player ID, one for the game level and
one for the score. All data is stored in the array as strings.
HighScores is declared local to the main program, and all elements are initialised to an
empty string, for example " ".
Write program code to declare and initialise HighScores.
Save your program as Question3_N24.
Copy and paste the program code into part 3(a) in the evidence document.
[2]
(b) The function ReadData() reads the data from HighScoreTable.txt and stores the data
in a 2D array. The function returns the 2D array.
The function uses exception handling when opening and reading data from the file.
Write program code for ReadData().
Save your program.
Copy and paste the program code into part 3(b) in the evidence document.
[5]
(c) The procedure OutputHighScores() takes a 2D array as a parameter. It outputs each
player’s ID, level and score in the order they are in the array. The outputs are in the format:
GHEH reached level 3 with a score of 10
Write program code for OutputHighScores().
Save your program.
Copy and paste the program code into part 3(c) in the evidence document.
[2]
(d) The scores in HighScoreTable.txt are not in order.
The player scores are first sorted by the game level they reached. For example, all players
that reached level 5 are higher in the high score table than players that reached level 4.
The players that reached the same level are then sorted in descending order of score.
An example sorted high score table is:
Position
Player ID
Game level
Score
1
GFED
5
25
2
HJKM
4
21
3
RERR
4
19
4
TTYU
4
15
5
WSVG
3
20
6
PPTR
3
15
7
SNQT
2
10
The function SortScores() uses a bubble sort to sort the data as described and returns the
sorted array.
Write program code for SortScores().
Save your program.
Copy and paste the program code into part 3(d) in the evidence document.
[4]
(e) (i)
Amend the main program to implement these steps in the order given:
•
call ReadData() and store the returned array in HighScores
•
output "Before"
•
call OutputHighScores() with HighScores as a parameter
•
call SortScores() and store the returned array in HighScores
•
output "After"
•
call OutputHighScores() with HighScores as a parameter.
Save your program.
Copy and paste the program code into part 3(e)(i) in the evidence document.
[2]
(ii)
Test your program
Take a screenshot of the output.
Save your program.
Copy and paste the screenshot into part 3(e)(ii) in the evidence document.
[2]
Permission to reproduce items where third-party owned material protected by copyright is included has been sought and cleared where possible. Every
publisher will be pleased to make amends at the earliest possible opportunity.
To avoid the issue of disclosure of answer-related information to candidates, all copyright acknowledgements are reproduced online in the Cambridge
Assessment International Education Copyright Acknowledgements Booklet. This is produced for each series of examinations and is freely available to download
at www.cambridgeinternational.org after the live examination series.
Cambridge Assessment International Education is part of Cambridge Assessment. Cambridge Assessment is the brand name of the University of Cambridge

Advertisement