Data types, records, arrays and abstract data types – A Level Computer Science (9618)

50 questions · page 2 of 4 · Clear filters

0 / 0Answer a question to start your score.
Quick quiz (10)Saved 0
Advertisement
16
9618/42/M/J/25 · Question 2May/June 2025Paper 4222 marks
Loading question…
Question text

A program stores data in a 1D array of records, HashTable. The array has space for 200 records.
2
Each data item is stored in a specific index of the array that is calculated using an algorithm. The
index is calculated from the key field using the formula: Key MOD 200
If two key fields generate the same index, there is a collision. Any records that have a collision
are stored in a second array, Spare. This array has space for 100 records. When a collision is
detected, the record is stored in the next free space in Spare.
(a) The pseudocode record format is:
TYPE NewRecord
DECLARE Key : INTEGER
DECLARE Item1 : INTEGER
DECLARE Item2 : INTEGER
ENDTYPE
Write program code to declare the record type NewRecord
If your chosen programming language does not support record formats, a class can be used
instead.
Save your program as Question2_J25.
Copy and paste the program code into part 2(a) in the evidence document.
[2]
Write program code to declare the global arrays HashTable and Spare
(b) (i)
Save your program.
Copy and paste the program code into part 2(b)(i) in the evidence document.
[1]
An empty record has the integer –1 stored in each field.
(ii)
The procedure Initialise() stores an empty record in each element in HashTable
and Spare
Write program code for Initialise()
Save your program.
Copy and paste the program code into part 2(b)(ii) in the evidence document.
[2]
(c) The hash value is calculated from the key field using the formula: Key MOD 200
The function CalculateHash() takes a key field as a parameter and calculates and returns
the hash value for the key field.
Write program code for CalculateHash()
Save your program.
Copy and paste the program code into part 2(c) in the evidence document.
[2]
(d) The procedure InsertIntoHash():
•
takes a record of type NewRecord as a parameter
•
uses CalculateHash() to calculate the hash value for the key field in the record
•
checks if the hash value index in HashTable currently stores an empty record
○ if the index stores an empty record, store the parameter in this index
○ if the index does not store an empty record, store the parameter in Spare
You can assume there will always be enough space in Spare to store any collisions.
Write program code for InsertIntoHash()
Save your program.
Copy and paste the program code into part 2(d) in the evidence document.
[6]
(e) The text file HashData.txt stores up to 200 rows of data to be stored into the hash table.
Each row contains three integer numbers separated by commas.
The first number is the key field, the second number is item 1 and the third number is item 2.
For example:
The first row in the text file contains: 646, 12, 568
The key field is 646, item 1 is 12 and item 2 is 568
The procedure CreateHashTable():
•
opens the file HashData.txt
•
creates a record for each row of data in the file
•
calls InsertIntoHash() with each record.
Write program code for CreateHashTable()
Save your program.
Copy and paste the program code into part 2(e) in the evidence document.
[5]
The procedure PrintSpare() outputs the key field of each element in the array Spare
(f)
(i)
that does not contain an empty record.
Write program code for PrintSpare()
Save your program.
Copy and paste the program code into part 2(f)(i) in the evidence document.
[2]
(ii)
The main program should call the procedure to initialise the arrays, call the procedure
to create the hash table and then call the procedure to output the contents of the array
Spare
Write program code for the main program.
Save your program.
Copy and paste the program code into part 2(f)(ii) in the evidence document.
[1]
(iii)
Test your program.
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 2(f)(iii) in the evidence document.
[1]

17
9618/43/M/J/25 · Question 1May/June 2025Paper 4326 marks
Loading question…
Question text

A program reads data from a text file and stores it in a queue. The linear queue Queue is stored as
1
a 1D array of up to 50 elements. The queue has the following pointers:
•
HeadPointer – this stores the index of the first element in the queue, initialised to –1
•
TailPointer – this stores the index of the last element in the queue, initialised to –1
(a) Queue is a global array of 50 integers with all elements initialised to –1 in the main program.
The two pointers are declared as global variables and initialised to –1
Write program code to declare and initialise Queue, HeadPointer and TailPointer
Save your program as Question1_J25.
Copy and paste the program code into part 1(a) in the evidence document.
[3]
(b) The function Enqueue():
•
takes an integer parameter to store in the next position in the queue
•
returns FALSE if the queue is full and the parameter cannot be stored in the queue
•
returns TRUE if the parameter is stored in the queue
•
updates the pointers where appropriate.
Write program code for Enqueue()
Save your program.
Copy and paste the program code into part 1(b) in the evidence document.
[6]
(c) The function Dequeue():
•
returns the next item in the queue, if the queue is not empty
•
returns –1 if the queue is empty
•
updates the pointers where appropriate.
Write program code for Dequeue()
Save your program.
Copy and paste the program code into part 1(c) in the evidence document.
[5]
(d) The text file QueueData.txt stores positive integers. Each integer is on a new line in the
file.
The procedure CreateQueue():
•
opens the file QueueData.txt
•
reads in each line from the text file and uses Enqueue() to insert each line into the
queue
•
outputs "Queue full" if any item cannot be inserted into the queue
•
uses exception handling when opening and reading from the text file.
The procedure needs to work for a file that contains an unknown number of lines.
Write program code for CreateQueue()
Save your program.
Copy and paste the program code into part 1(d) in the evidence document.
[6]
The main program needs extending to call CreateQueue(). The main program then
(e) (i)
adds together all of the integers stored in the queue, using Dequeue() to access each
integer. The total is then output.
Write program code to extend the main program.
Save your program.
Copy and paste the program code into part 1(e)(i) in the evidence document.
[5]
(ii)
Test your program.
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 1(e)(ii) in the evidence document.
[1]

18
9618/43/M/J/25 · Question 2May/June 2025Paper 4320 marks
Loading question…
Question text

A program sorts the data in the 1D array DataArray and searches DataArray for specific
2
values.
DataArray stores 14 integer values and is declared local to the main program.
(a) Write program code to create DataArray and initialise it with the following data values in the
order they are written:
0
3
4
56
67
44
43
32
31
345
45
6
54
1
Save your program as Question2_J25.
Copy and paste the program code into part 2(a) in the evidence document.
[1]
(b) The function InsertionSort() takes an array of integers as a parameter. The function
sorts the data in the array into ascending numerical order using an insertion sort. The function
returns the sorted array.
Write program code for InsertionSort()
You must not use any inbuilt sorting functions for your programming language.
Save your program.
Copy and paste the program code into part 2(b) in the evidence document.
[5]
(c) The procedure OutputArray() takes an array of integers as a parameter. The procedure
outputs each element in the array from the first element to the last element. The output is on
one line with a space between each number.
An example output is:
"0 3 4 56 67 44 43 32 31 345 45 6 54 1"
Write program code for OutputArray()
Save your program.
Copy and paste the program code into part 2(c) in the evidence document.
[2]
(d) (i)
The main program needs extending to:
•
output the content of the unsorted array using OutputArray()
•
sort the array using InsertionSort()
•
output the content of the sorted array using OutputArray()
Write program code to extend the main program.
Save your program.
Copy and paste the program code into part 2(d)(i) in the evidence document.
[2]
(ii)
Test your program.
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 2(d)(ii) in the evidence document.
[1]
(e) The function Search() performs a binary search to find ItemToFind in DataArray
The function takes two parameters:
•
DataArray, an array of integers
•
ItemToFind, an integer to find in DataArray
The function returns:
•
the index of ItemToFind if it is in DataArray
•
–1 if ItemToFind is not in DataArray
Write program code for Search()
You must not use any inbuilt searching functions for your programming language.
Save your program.
Copy and paste the program code into part 2(e) in the evidence document.
[6]
The main program needs extending to call Search() with the sorted array four times:
(f)
(i)
•
the first time to find the index of the integer 0
•
the second time to find the index of the integer 345
•
the third time to find the index of the integer 67
•
the fourth time to find the index of the integer 2
If the integer is found in the array, output an appropriate message that includes the index.
If the integer is not found, output that it was not found.
Write program code to extend the main program.
Save your program.
Copy and paste the program code into part 2(f)(i) in the evidence document.
[2]
(ii)
Test your program.
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 2(f)(ii) in the evidence document.
[1]

Advertisement
19
9618/41/O/N/24 · Question 1Oct/Nov 2024Paper 4122 marks
Loading question…
Question text

1
A program sorts string data using different sorting methods.
(a) The text file Data.txt stores string data items. Each data item is on a new line in the text
file.
The function ReadData():
•
has a local array of strings that can store 45 items
•
reads each line of data and stores it in the array
•
returns the array.
Write program code for the function ReadData().
Save your program as Question1_N24.
Copy and paste the program code into part 1(a) in the evidence document.
[6]
(b) The function FormatArray() takes an array of strings as a parameter. It concatenates the
contents of the array into one string with a space between each array element. The function
returns the concatenated string.
Write program code for FormatArray().
(i)
Save your program.
Copy and paste the program code into part 1(b)(i) in the evidence document.
[2]
(ii)
The main program:
•
calls ReadData() and stores the returned array
•
calls FormatArray() with the returned array and outputs the returned string.
Write program code for the main program.
Save your program.
Copy and paste the program code into part 1(b)(ii) in the evidence document.
[3]
(iii)
Test your program.
Take a screenshot of the output.
Save your program.
Copy and paste the screenshot into part 1(b)(iii) in the evidence document.
[1]
(c) The function CompareStrings():
•
takes two strings as parameters
•
compares each string, one character at a time, to identify which string comes first
alphabetically. If the first two characters are the same, the second character of each
string is compared. This continues until the two characters are different.
The function:
•
returns 1 if the first parameter comes before the second alphabetically
•
returns 2 if the second parameter comes before the first alphabetically.
Write program code for CompareStrings().
Assume that all strings are in lower case.
Assume that a difference between two strings will always be identified before the end of one
string is reached.
Do not use an in-built string comparison function.
The strings must be compared one character at a time.
Save your program.
Copy and paste the program code into part 1(c) in the evidence document.
[4]
(d) The function Bubble() takes an array of strings as a parameter and sorts the data into
ascending alphabetical order, using a bubble sort. The bubble sort uses CompareStrings()
to compare each string.
The function returns the sorted list.
Write program code for Bubble().
(i)
Save your program.
Copy and paste the program code into part 1(d)(i) in the evidence document.
[3]
(ii)
Write program code to amend the main program to:
•
call Bubble() with the unsorted array as a parameter
•
call FormatArray() with the sorted array and output the returned string.
Save your program.
Copy and paste the program code into part 1(d)(ii) in the evidence document.
[2]
(iii)
Test your program.
Take a screenshot of the output.
Save your program.
Copy and paste the screenshot into part 1(d)(iii) in the evidence document.
[1]

20
9618/41/O/N/24 · Question 3Oct/Nov 2024Paper 4119 marks
Loading question…
Question text

3
A linked list stores positive integer data in a 2D array. The first dimension of the array stores the
integer data. The second dimension of the array stores the pointer to the next node in the linked
list.
A linked list node with no data is initialised with the integer –1. These nodes are linked together as
an empty list. A pointer of –1 identifies that node as the last node.
The linked list can store 20 nodes.
The global 2D array LinkedList stores the linked list.
LinkedList is initialised as an empty list. The data in each node is initialised to –1. Each node’s
pointer stores the index of the next node. The last node stores the pointer value –1, which indicates
it is the last node.
The global variable FirstEmpty stores the index of the first element in the empty list. This is the
first node in the empty linked list when it is initialised, which is index 0.
The global variable FirstNode stores the index of the first element in the linked list. There is no
data in the linked list when it is initialised, so FirstNode is initialised to –1.
This diagram shows the content of the initialised array.
FirstEmpty = 0
FirstNode = –1
Index
Data
Pointer
0
-1
1
1
-1
2
2
-1
3
3
-1
4
4
-1
5
19
-1
-1
(a) Write program code for the main program to declare and initialise LinkedList, FirstNode
and FirstEmpty.
Save your program as Question3_N24.
Copy and paste the program code into part 3(a) in the evidence document.
[2]
(b) The procedure InsertData() takes five positive integers as input from the user and inserts
these into the linked list.
Each data item is inserted at the front of the linked list.
The table shows the steps to follow depending on the state of the linked list:
Linked list state
Steps
insert the data in the index pointed to by FirstEmpty
change the pointer to the index pointed to by FirstNode
not full
change the values of FirstNode and FirstEmpty
full
end the procedure
Any node that is at the end of the linked list has a pointer of –1.
Write program code for InsertData().
Save your program.
Copy and paste the program code into part 3(b) in the evidence document.
[6]
(c) The procedure OutputLinkedList() outputs the data in the linked list in order by following
the pointers from FirstNode.
Write program code for OutputLinkedList().
(i)
Save your program.
Copy and paste the program code into part 3(c)(i) in the evidence document.
[2]
Amend the main program to call InsertData() and then OutputLinkedList().
(ii)
Save your program.
Copy and paste the program code into part 3(c)(ii) in the evidence document.
[1]
(iii)
Test your program with the test data:
5
1
2
3
8
Take a screenshot of the output.
Save your program.
Copy and paste the screenshot into part 3(c)(iii) in the evidence document.
[1]
(d) The procedure RemoveData() removes a node from the linked list.
The procedure takes the data item to be removed from the linked list as a parameter.
The procedure checks each node in the linked list, starting with the node FirstNode, until it
finds the node to be removed. This node is added to the empty list, and pointers are changed
as appropriate. The procedure only removes the first occurrence of the parameter.
Assume that the data item being removed is in the linked list.
Write program code for RemoveData().
(i)
Save your program.
Copy and paste the program code into part 3(d)(i) in the evidence document.
[5]
(ii)
Amend the main program to:
•
call RemoveData() with the parameter 5
•
output the word "After"
•
call OutputLinkedList().
Save your program.
Copy and paste the program code into part 3(d)(ii) in the evidence document.
[1]
(iii)
Test your program with both sets of given test data:
Test data set 1:
5
6
8
9
5
Test data set 2:
10
7
8
5
6
Take a screenshot of each output.
Save your program.
Copy and paste the screenshot(s) into part 3(d)(iii) in the evidence document.
[1]
Permission to reproduce items where third-party owned material protected by copyr

21
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/

22
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

23
9618/41/M/J/24 · Question 1May/June 2024Paper 4127 marks
Loading question…
Question text

1
A program needs to take integer numbers as input, sort the numbers and then search for a specific
number.
(a) The integer numbers will be stored in the global 1D array, DataStored, with space for up to
20 integers.
The global variable NumberItems stores the quantity of items the array contains.
Write program code to declare DataStored and NumberItems.
Save your program as Question1_J24.
Copy and paste the program code into part 1(a) in the evidence document.
[1]
(b) The procedure Initialise():
•
prompts the user to input the quantity of numbers the user would like to enter
•
reads the input and validates it is between 1 and 20 (inclusive)
•
prompts the user to input each number and stores each number in DataStored.
Write program code for Initialise().
Save your program.
Copy and paste the program code into part 1(b) in the evidence document.
[5]
(c) The main program stores 0 in NumberItems, calls Initialise() and then outputs the
contents of DataStored.
(i)
Write program code for the main program.
Save your program.
Copy and paste the program code into part 1(c)(i) in the evidence document.
[2]
(ii)
Test your program by inputting the following data in the order given:
30
5
3
9
4
1
2
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 1(c)(ii) in the evidence document.
[2]
(d) The procedure BubbleSort() uses a bubble sort to sort the data in DataStored into
ascending numerical order.
Write program code for BubbleSort().
(i)
Save your program.
Copy and paste the program code into part 1(d)(i) in the evidence document.
[4]
Write program code to amend the main program to call BubbleSort() and then output
(ii)
the contents of DataStored.
Save your program.
Copy and paste the program code into part 1(d)(ii) in the evidence document.
[1]
(iii)
Test your program by inputting the following data in the order given:
5
3
9
4
1
2
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 1(d)(iii) in the evidence document.
[1]
(e) The function BinarySearch():
•
takes the integer parameter DataToFind to search for in the array
•
performs an iterative binary search on the array DataStored
•
returns the index where DataToFind is found in DataStored. If DataToFind is not
found, the function returns −1.
Write program code for the iterative function BinarySearch().
(i)
Save your program.
Copy and paste the program code into part 1(e)(i) in the evidence document.
[6]
(ii)
Write program code to amend the main program to:
•
take a number as input from the user
•
call BinarySearch() with the number input
•
output the value returned from the function call as its parameter.
Save your program.
Copy and paste the program code into part 1(e)(ii) in the evidence document.
[3]
(iii)
Test your program twice with the following inputs:
Test 1: 5
1
6
2
8
10
2
Test 2: 5
1
6
2
8
10
7
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 1(e)(iii) in the evidence document.
[2]

Advertisement
24
9618/41/M/J/24 · Question 3May/June 2024Paper 4117 marks
Loading question…
Question text

3
A program reads data from the user and stores the data that is valid in a linear queue.
The queue is stored as a global 1D array, QueueData, of string values. The array needs space for
20 elements.
The global variable QueueHead stores the index of the first element in the queue.
The global variable QueueTail stores the index of the last element in the queue.
(a) The main program initialises all the elements in QueueData to a suitable null value,
QueueHead to −1 and QueueTail to −1.
Write program code for the main program.
Save your program as Question3_J24.
Copy and paste the program code into part 3(a) in the evidence document.
[1]
(b) The function Enqueue() takes the data to insert into the queue as a parameter.
If the queue is not full, it inserts the parameter in the queue, updates the appropriate pointer(s)
and returns TRUE. If the queue is full, it returns FALSE.
Write program code for Enqueue().
Save your program.
Copy and paste the program code into part 3(b) in the evidence document.
[4]
(c) The function Dequeue() returns "false" if the queue is empty. If the queue is not empty, it
returns the next item in the queue and updates the appropriate pointer(s).
Write program code for Dequeue().
Save your program.
Copy and paste the program code into part 3(c) in the evidence document.
[3]
(d) The string values to be stored in the queue are 7 characters long. The first 6 characters are
digits and the 7th character is a check digit. The check digit is calculated from the first 6 digits
using this algorithm:
•
multiply the digits in position 0, position 2 and position 4 by 1
•
multiply the digits in position 1, position 3 and position 5 by 3
•
calculate the sum of the products (add together the results from all of the multiplications)
•
divide the sum of the products by 10 and round the result down to the nearest integer to
get the check digit
•
if the check digit equals 10 then it is replaced with 'X'.
Example:
Data is 954123
Character position
0
1
2
3
4
5
Digit
9
5
4
1
2
3
Multiplier
1
3
1
3
1
3
Product
9
15
4
3
2
9
Sum of products = 9 + 15 + 4 + 3 + 2 + 9 = 42
Divide sum of products by 10: 42 / 10 = 4 (rounded down)
The check digit = 4. This is inserted into character position 6.
The data including the check digit is: 9541234
A 7‑character string is valid if the 7th character matches the check digit for that data. For
example, the data 9541235 is invalid because the 7th character (5) does not match the check
digit for 954123.
The subroutine StoreItems() takes ten 7‑character strings as input from the user and
(i)
uses the check digit to validate each input.
Each valid input has the check digit removed and is stored in the queue using
Enqueue().
An appropriate message is output if the item is inserted. An appropriate message is
output if the queue is already full.
Invalid inputs are not stored in the queue.
The subroutine counts and outputs the number of invalid items that were entered.
StoreItems() can be a procedure or a function as appropriate.
Write program code for StoreItems().
Save your program.
Copy and paste the program code into part 3(d)(i) in the evidence document.
[6]
(ii)
Write program code to amend the main program to:
•
call StoreItems()
•
call Dequeue()
•
output a suitable message if the queue was empty
•
output the returned value if the queue was not empty.
Save your program.
Copy and paste the program code into part 3(d)(ii) in the evidence document.
[1]
(iii)
Test the program with the following inputs in the order given:
999999X
1251484
5500212
0033585
9845788
6666666
3258746
8111022
7568557
0012353
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 3(d)(iii) 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 poss

25
9618/42/M/J/24 · Question 1May/June 2024Paper 4222 marks
Loading question…
Question text

1
A program outputs a main word. The program asks the user to enter the different words of 3 or
more letters that can be made from the letters in the main word. These are called the answers.
There are 3 files: Easy.txt, Medium.txt and Hard.txt. Each file has the main word on the
first line. For example, the main word in Easy.txt is house.
The answers are stored in the file. Each answer is on a new line after the main word. For example,
Easy.txt has 14 answers that can be made from the letters in house.
The words read from the text file are stored in the global array WordArray. The number of words
that can be made from the letters in the main word is stored in the global variable NumberWords.
(a) The procedure ReadWords():
•
takes a file name as a parameter
•
opens the file and reads in the data
•
stores the main word in the first element in WordArray
•
stores each answer in a new element in WordArray
•
counts and stores the number of answers.
Write program code for the procedure ReadWords().
Save your program as Question1_J24.
Copy and paste the program code into part 1(a) in the evidence document.
[6]
(b) The main program asks the user to enter "easy", "medium" or "hard" and calls
ReadWords()with the filename that matches the user’s input. For example, if the user enters
"easy", the parameter is "Easy.txt".
Write program code for the main program.
Save your program.
Copy and paste the program code into part 1(b) in the evidence document.
[4]
The procedure Play():
(c) (i)
•
outputs the main word from the array and the number of answers
•
allows the user to enter words until they enter the word ‘no’ to indicate they want to
stop
•
outputs whether each word the user enters is an answer or not an answer
•
counts the number of answers the user gets correct
•
replaces each answer that the user gets correct with a null value in the array.
Write program code for the procedure Play().
Save your program.
Copy and paste the program code into part 1(c)(i) in the evidence document.
[6]
Amend the procedure Play()so that when the user enters the command to stop, the
(ii)
procedure:
•
outputs the percentage of answers the user entered from the array
•
outputs all the answers that the user did not enter.
Write program code to amend Play().
Save your program.
Copy and paste the program code into part 1(c)(ii) in the evidence document.
[3]
The procedure ReadWords()calls Play()after the data in the file has been read.
(d) (i)
Write program code to amend ReadWords().
Save your program.
Copy and paste the program code into part 1(d)(i) in the evidence document.
[1]
(ii)
Test your program by inputting these words in the order shown:
easy
she
out
no
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 1(d)(ii) in the evidence document.
[1]
(iii)
Test your program by inputting these words in the order shown:
hard
fine
fined
idea
no
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 1(d)(iii) in the evidence document.
[1]

26
9618/42/M/J/24 · Question 2May/June 2024Paper 4230 marks
Loading question…
Question text

2
A binary tree stores data in ascending order. For example:
15
8
19
3
10
A computer program stores integers in a binary tree in ascending order. The program uses
Object‑Oriented Programming (OOP).
The binary tree is stored as a 1D array of nodes. Each node contains a left pointer, a data value
and a right pointer.
The class Node stores the data about a node.
Node
LeftPointer : INTEGER
stores the index of the node to the left in the binary tree
Data : INTEGER
stores the node’s data
RightPointer : INTEGER
stores the index of the node to the right in the binary tree
Constructor()
initialises Data to its parameter value
initialises LeftPointer and RightPointer to −1
GetLeft()
returns the left pointer
GetRight()
returns the right pointer
GetData()
returns the data value
SetLeft()
assigns the parameter to the left pointer
SetRight()
assigns the parameter to the right pointer
SetData()
assigns the parameter to the data
Write program code to declare the class Node and its constructor.
(a) (i)
Do not declare the other methods.
Use the appropriate constructor for your programming language.
If you are writing in Python, include attribute declarations using comments.
Save your program as Question2_J24.
Copy and paste the program code into part 2(a)(i) in the evidence document.
[4]
The get methods GetLeft(), GetRight()and GetData()each return the relevant
(ii)
attribute.
Write program code for the three get methods.
Save your program.
Copy and paste the program code into part 2(a)(ii) in the evidence document.
[3]
The set methods SetLeft(), SetRight()and SetData()each take a parameter and
(iii)
then store this in the relevant attribute.
Write program code for the three set methods.
Save your program.
Copy and paste the program code into part 2(a)(iii) in the evidence document.
[3]
(b) The class TreeClass stores the data about the binary tree.
TreeClass
Tree[0:19] : Node
an array of 20 elements of type Node
FirstNode : INTEGER
stores the index of the first node in the tree
NumberNodes : INTEGER
stores the quantity of nodes in the tree
Constructor()
initialises FirstNode to −1 and NumberNodes to 0
initialises each element in Tree to a Node object with the
data value of −1
InsertNode()
takes a Node object as a parameter, inserts it in the array
and updates the pointer for its parent node
OutputTree()
outputs the left pointer, data and right pointer of each
node in Tree
Nodes cannot be deleted from this tree.
Write program code to declare the class TreeClass and its constructor.
(i)
Do not declare the other methods.
Use the appropriate constructor for your programming language.
If you are writing in Python, include attribute declarations using comments.
Save your program.
Copy and paste the program code into part 2(b)(i) in the evidence document.
[4]
The method InsertNode()takes a Node object, NewNode, as a parameter and inserts
(ii)
it into the array Tree.
InsertNode()first checks if the tree is empty.
If the tree is empty, InsertNode():
•
stores NewNode in the array Tree at index NumberNodes
•
increments NumberNodes
•
stores 0 in FirstNode.
If the tree is not empty, InsertNode():
•
stores NewNode in the array Tree at index NumberNodes
•
accesses the data in the array Tree at index FirstNode and compares it to the
data in NewNode
•
repeatedly follows the pointers until the correct position for NewNode is found
•
once the position is found, InsertNode()sets the left or right pointer of its parent
node
•
increments NumberNodes.
Write program code for InsertNode().
Save your program.
Copy and paste the program code into part 2(b)(ii) in the evidence document.
[6]
The method OutputTree()outputs the left pointer, the data and the right pointer for
(iii)
each node that has been inserted into the tree. The outputs are in the order they are
saved in the array.
If there are no nodes in the array, the procedure outputs ‘No nodes’.
Write program code for OutputTree().
Save your program.
Copy and paste the progra

27
9618/42/M/J/24 · Question 3May/June 2024Paper 4223 marks
Loading question…
Question text

3
A program sorts an array of integers and searches the array for a particular value.
(a) The array of integers, NumberArray, stores the following data in the order given:
100
85
644
22
15
8
1
The array is declared and initialised local to the main program.
Write program code to declare and initialise the array.
Save your program as Question3_J24.
Copy and paste the program code into part 3(a) in the evidence document.
[1]
(b) (i)
The following recursive pseudocode function sorts the array into ascending order using
an insertion sort and returns the sorted array.
DECLARE LastItem : INTEGER
DECLARE CheckItem : INTEGER
DECLARE LoopAgain : BOOLEAN
FUNCTION RecursiveInsertion(IntegerArray : ARRAY[] OF INTEGER,
NumberElements : INTEGER) RETURNS ARRAY[] OF INTEGER
IF NumberElements <= 1 THEN
RETURN IntegerArray
ELSE
CALL RecursiveInsertion(IntegerArray, NumberElements - 1)
LastItem
IntegerArray[NumberElements - 1]
CheckItem
NumberElements - 2
ENDIF
LoopAgain
TRUE
IF CheckItem < 0 THEN
LoopAgain
FALSE
ELSE
IF IntegerArray[CheckItem] < LastItem THEN
LoopAgain
FALSE
ENDIF
ENDIF
WHILE LoopAgain
IntegerArray[CheckItem + 1]
IntegerArray[CheckItem]
CheckItem
CheckItem - 1
IF CheckItem < 0 THEN
LoopAgain
FALSE
ELSE
IF IntegerArray[CheckItem] < LastItem THEN
LoopAgain
FALSE
ENDIF
ENDIF
ENDWHILE
IntegerArray[CheckItem + 1]
LastItem
RETURN IntegerArray
ENDFUNCTION
Write the program code for the pseudocode function RecursiveInsertion().
Save your program.
Copy and paste the program code into part 3(b)(i) in the evidence document.
[4]
(ii)
Amend the main program to:
•
call RecursiveInsertion()with the initialised array NumberArray and the
number of elements as parameters
•
output the word ‘Recursive’
•
output the content of the returned array.
Save your program.
Copy and paste the program code into part 3(b)(ii) in the evidence document.
[2]
(iii)
Test your program.
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 3(b)(iii) in the evidence document.
[1]
(c) The function RecursiveInsertion()can be changed to use iteration instead of recursion.
Write program code for the function IterativeInsertion()to perform the same
(i)
processes as RecursiveInsertion()but using iteration instead of recursion.
Save your program.
Copy and paste the program code into part 3(c)(i) in the evidence document.
[4]
(ii)
Write program code to amend the main program to also:
•
call IterativeInsertion()with the original initialised array NumberArray
•
output the word ‘iterative’
•
output the content of the returned array.
Save your program.
Copy and paste the program code into part 3(c)(ii) in the evidence document.
[1]
(iii)
Test your program.
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 3(c)(iii) in the evidence document.
[1]
(d) The recursive function BinarySearch()takes the parameters:
•
IntegerArray – an array of integers
•
First – the index of the first array element
•
Last – the index of the last array element
•
ToFind – an integer to search for in the array.
The function uses recursion to perform a binary search for ToFind in IntegerArray.
The function returns the index where ToFind is stored or returns −1 if ToFind is not in the
array.
Write program code for the recursive function BinarySearch().
(i)
Save your program.
Copy and paste the program code into part 3(d)(i) in the evidence document.
[6]
(ii)
Write program code to amend the main program to:
•
call BinarySearch()with the sorted array and the integer 644 as the search value
•
output ‘Not found’ if 644 is not found in the array
•
output the index if 644 is found in the array.
Save your program.
Copy and paste the program code into part 3(d)(ii) in the evidence document.
[2]
(iii)
Test your program.
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 3(d)(iii) in the evidence document.
[1]

28
9618/41/O/N/23 · Question 1Oct/Nov 2023Paper 4116 marks
Loading question…
Question text

1
This iterative pseudocode algorithm for the function IterativeVowels() takes a string as a
parameter and counts the number of lower-case vowels in this string.
The vowels are the letters a, e, i, o and u.
FUNCTION IterativeVowels(Value : STRING) RETURNS INTEGER
DECLARE Total : INTEGER
DECLARE LengthString : INTEGER
DECLARE FirstCharacter : CHAR
← 0
Total
← LENGTH(Value)
LengthString
← 0 TO LengthString - 1
FOR X
FirstCharacter ← MID(Value, 0, 1)
IF FirstCharacter = 'a' OR FirstCharacter = 'e' OR
FirstCharacter = 'i' OR FirstCharacter = 'o' OR
FirstCharacter = 'u' THEN
← Total + 1
Total
ENDIF
← MID(Value, 1, LENGTH(Value)-1)
Value
NEXT X
RETURN Total
ENDFUNCTION
The pseudocode function MID(X, Y, Z) returns Z number of characters from string X, starting
at the character in position Y. The first character in a string is in position 0, for example:
MID("computer", 0, 3) returns "com"
The pseudocode function LENGTH(X) returns the number of characters in the string X, for
example:
LENGTH("computer") returns 8
(a) (i)
Write program code for the function IterativeVowels().
Save your program as Question1_N23.
Copy and paste the program code into part 1(a)(i) in the evidence document.
[5]
(ii)
Write program code to call the function IterativeVowels() with the parameter
"house" from the main program.
Output the return value.
Save your program.
Copy and paste the program code into part 1(a)(ii) in the evidence document.
[2]
(iii)
Test your program.
Take a screenshot of the output.
Save your program.
Copy and paste the screenshot into part 1(a)(iii) in the evidence document.
[1]
(b) (i)
Rewrite the function IterativeVowels() as a recursive function with the identifier
RecursiveVowels().
Save your program.
Copy and paste the program code into part 1(b)(i) in the evidence document.
[6]
(ii)
Write program code to call the function RecursiveVowels() with the parameter
"imagine" from the main program.
Output the return value.
Save your program.
Copy and paste the program code into part 1(b)(ii) in the evidence document.
[1]
(iii)
Test your program.
Take a screenshot of the output.
Save your program.
Copy and paste the screenshot into part 1(b)(iii) in the evidence document.
[1]

Advertisement
29
9618/41/O/N/23 · Question 2Oct/Nov 2023Paper 4129 marks
Loading question…
Question text

2
A linear queue is implemented using the 1D array, Queue. The index of the first element in the
array is 0.
(a) (i)
Write program code to declare:
•
Queue — a global array with space to store 50 IDs of type string
•
HeadPointer — a global variable to point to the first element in the queue,
initialised to −1
•
TailPointer — a global variable to point to the next available space in the queue,
initialised to 0.
Save your program as Question2_N23.
Copy and paste the program code into part 2(a)(i) in the evidence document.
[2]
(ii)
The procedure Enqueue() takes a string parameter.
If the queue is full, the procedure outputs a suitable message. If the queue is not full, the
procedure inserts the parameter into the queue and updates the relevant pointer(s).
Write program code for Enqueue().
Save your program.
Copy and paste the program code into part 2(a)(ii) in the evidence document.
[4]
(iii)
The function Dequeue() checks if the queue is empty.
If the queue is empty, the function outputs a suitable message and returns the string
"Empty".
If the queue is not empty, the function returns the first element in the queue and updates
the relevant pointer(s).
Write program code for Dequeue().
Save your program.
Copy and paste the program code into part 2(a)(iii) in the evidence document.
[4]
(b) A shop sells computer games. Each game has a unique identifier (ID) of string data type.
The text file QueueData.txt contains a list of game IDs.
The procedure ReadData() reads the data from the text file and inserts each item of data
into the array Queue.
Write program code for the procedure ReadData().
Save your program.
Copy and paste the program code into part 2(b) in the evidence document.
[6]
(c) Some game IDs appear in the text file more than once.
The program needs to total the number of times each game ID appears in the text file.
The record structure RecordData has the following fields:
•
ID — a string to store the game ID
•
Total — an integer to store the total number of times that game ID appears in the text
file.
(i)
Write program code to declare the record structure RecordData.
If you are writing in Python, include attribute declarations as comments.
Save your program.
Copy and paste the program code into part 2(c)(i) in the evidence document.
[2]
(ii)
The global 1D array Records stores up to 50 items of type RecordData.
The global variable NumberRecords stores the number of records currently in the array
Records and is initialised to 0.
Write program code to declare Records and NumberRecords.
If you are writing in Python, include attribute declarations as comments.
Save your program.
Copy and paste the program code into part 2(c)(ii) in the evidence document.
[2]
(iii)
The pseudocode algorithm for the procedure TotalData():
•
uses Dequeue() to remove an ID from the queue
•
checks whether a RecordData with the returned ID exists in Records
•
increments the total for that ID in the record if the ID already exists
•
creates a new record and stores it in Records if the ID does not exist.
PROCEDURE TotalData()
DECLARE DataAccessed : STRING
DECLARE Flag : BOOLEAN
← Dequeue()
DataAccessed
← FALSE
Flag
IF NumberRecords = 0 THEN
← DataAccessed
Records[NumberRecords].ID
Records[NumberRecords].Total ← 1
Flag ← TRUE
NumberRecords ← NumberRecords + 1
ELSE
← 0 TO NumberRecords - 1
FOR X
IF Records[X].ID = DataAccessed THEN
← Records[X].Total + 1
Records[X].Total
← TRUE
Flag
ENDIF
NEXT X
ENDIF
IF Flag = FALSE THEN
← DataAccessed
Records[NumberRecords].ID
Records[NumberRecords].Total ← 1
NumberRecords ← NumberRecords + 1
ENDIF
ENDPROCEDURE
Write program code for the procedure TotalData().
Save your program.
Copy and paste the program code into part 2(c)(iii) in the evidence document.
[5]
(d) The procedure OutputRecords() outputs the ID and total of each record in Records in the
format:
ID
1234
Total
4
Write program code for OutputRecords().
Save your program.
Copy and paste the program code i

30
9618/42/O/N/23 · Question 1Oct/Nov 2023Paper 4227 marks
Loading question…
Question text

1
A program stores lower-case letters in two stacks.
One stack stores vowels (a, e, i, o, u) and one stack stores consonants (letters that are not
vowels).
Each stack is implemented as a 1D array.
(a) (i)
Write program code to declare two 1D global arrays: StackVowel and StackConsonant.
Each array needs to store up to 100 letters. The index of the first element in each array is 0.
If you are writing in Python, include declarations using comments.
Save your program as Question1_N23.
Copy and paste the program code into part 1(a)(i) in the evidence document.
[2]
(ii)
The global variable VowelTop is a pointer that stores the index of the next free space in
StackVowel.
The global variable ConsonantTop is a pointer that stores the index of the next free
space in StackConsonant.
VowelTop and ConsonantTop are both initialised to 0.
Write program code to declare and initialise the two variables.
If you are writing in Python, include declarations using comments.
Save your program.
Copy and paste the program code into part 1(a)(ii) in the evidence document.
[1]
(b) (i)
The procedure PushData() takes one letter as a parameter.
If the parameter is a vowel, it is pushed onto StackVowel and the relevant pointer
updated.
If the stack is full, a suitable message is output.
If the parameter is a consonant, it is pushed onto StackConsonant and the relevant
pointer updated.
If the stack is full, a suitable message is output.
You do not need to validate that the parameter is a letter.
Write program code for PushData().
Save your program.
Copy and paste the program code into part 1(b)(i) in the evidence document.
[6]
(ii)
The file StackData.txt stores 100 lower-case letters.
The procedure ReadData() reads each letter from the file and uses PushData() to
push each letter onto its appropriate stack.
Use appropriate exception handling if the file does not exist.
Write program code for ReadData().
Save your program.
Copy and paste the program code into part 1(b)(ii) in the evidence document.
[6]
(c) The function PopVowel() removes and returns the data at the top of StackVowel and
updates the relevant pointer(s).
The function PopConsonant() removes and returns the data from the top of StackConsonant
and updates the relevant pointer(s).
If either stack is empty, the string "No data" must be returned.
Write program code to declare PopVowel() and PopConsonant().
Save your program.
Copy and paste the program code into part 1(c) in the evidence document.
[5]
(d) The program first needs to call ReadData() and then:
1.
prompt the user to input their choice of vowel or consonant
2.
take, as input, the user’s choice
3.
depending on the user’s choice, call PopVowel() or PopConsonant() and store the
return value.
The three steps are repeated until 5 letters have been successfully returned and stored.
If either stack is empty at any stage, an appropriate message must be output.
Once 5 letters have been successfully returned and stored, they are output on one line, for
example:
abyti
(i)
Write program code for the main program.
Save your program.
Copy and paste the program code into part 1(d)(i) in the evidence document.
[6]
(ii)
Test your program with the following inputs:
vowel
consonant
consonant
vowel
vowel
Take a screenshot of the output.
Save your program.
Copy and paste the screenshot into part 1(d)(ii) in the evidence document.
[1]

Advertisement