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

22 questions · page 1 of 2 · Clear filters

0 / 0Answer a question to start your score.
Quick quiz (10)Saved 0
Advertisement
1
9618/41/O/N/25 · Question 1Oct/Nov 2025Paper 4120 marks
Loading question…
Question text

1
A program stores integers in a stack. The stack is represented as a 1D array of 30 elements with
the identifier Stack
The global pointer TopOfStack stores the index of the last element inserted into the stack.
TopOfStack is initialised to –1
(a) Write program code to declare Stack, initialise each element in the array with a null value
and declare and initialise TopOfStack
Save your program as Question1_N25.
Copy and paste the program code into part 1(a) in the evidence document.
[2]
(b) The function Push() takes an integer parameter. If the stack is full, the function returns
FALSE. If the stack is not full, the parameter is inserted into the stack, the pointer is updated
and the function returns TRUE
Write the program code for Push()
Save your program.
Copy and paste the program code into part 1(b) in the evidence document.
[4]
(c) The function Pop() returns the next integer in the stack and updates the pointer as
appropriate. If there is no data in the stack, the function returns the value –999
Write program code for Pop()
Save your program.
Copy and paste the program code into part 1(c) in the evidence document.
[4]
(d) The main program generates 40 random integers between 0 and 1000 (inclusive) and attempts
to insert each one into the stack using the appropriate function. If the return value from the
function call indicates the stack is full, no more integers are generated and "Stack full" is
output.
Write program code for the main program.
Save your program.
Copy and paste the program code into part 1(d) in the evidence document.
[4]
(e) The procedure FindValues():
•
pops each integer from the stack until the stack is empty
•
finds and outputs the largest number that was in the stack in an appropriate message
•
finds and outputs the smallest number that was in the stack in an appropriate message.
Write program code for FindValues()
Save your program.
Copy and paste the program code into part 1(e) in the evidence document.
[4]
Extend the main program to call FindValues()
(f)
(i)
Save your program.
Copy and paste the program code into part 1(f)(i) in the evidence document.
[1]
(ii)
Test your program.
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot(s) into part 1(f)(ii) in the evidence document.
[1]

2
9618/41/O/N/25 · Question 3Oct/Nov 2025Paper 4125 marks
Loading question…
Question text

A program stores records in the 2D array HashTable. Each record is stored at a specific index of
3
the array that is calculated using a hashing algorithm with the record’s key field.
The array has 100 × 10 elements. The hashing algorithm uses the key to generate an index
between 0 and 99 (inclusive). If two key fields generate the same index, there is a collision. Any
records that have a collision are stored in the next space in the same index.
For example: In this table two record keys generated the same hash value of 1. Four record keys
generated the same hash value of 3.
Index
0
1
2
3
4
5
…
9
0
record
1
record
record
2
3
record
record
record
record
4
…
99
The program uses Object-Oriented Programming (OOP).
(a) The class Record stores data about the records:
Record
Key : Integer
stores the integer key field for the data
Data : String
stores the string data
initialises Key and Data to its parameter values
Constructor()
The attributes Key and Data are public.
Write program code to declare the class Record and its constructor.
Use your programming language appropriate constructor.
Save your program as Question3_N25.
Copy and paste the program code into part 3(a) in the evidence document.
[2]
(b) The procedure InitialiseHashTable() initialises each element in the array to an empty
or null record.
Write program code to declare the global 2D array HashTable and the procedure
InitialiseHashTable()
Save your program.
Copy and paste the program code into part 3(b) in the evidence document.
[2]
(c) The function Hash():
•
takes an integer key field as a parameter
•
calculates and returns the hash value of the key field.
The hash value is the result from the formula: key MOD 100
Write program code for Hash()
Save your program.
Copy and paste the program code into part 3(c) in the evidence document.
[2]
(d) The procedure InsertData():
•
takes an object of type Record as a parameter
•
calculates the hash value for the parameter using the appropriate function
•
stores the parameter in the correct position in HashTable
You can assume there will be no more than 10 objects that generate the same hash value.
Write program code for InsertData()
Save your program.
Copy and paste the program code into part 3(d) in the evidence document.
[4]
(e) The file "HashTableData.txt" stores 200 key values and string data items in the format:
key,string
For example, the first row in the text file is:
528,permission
The key is 528 and the string data is "permission"
The procedure ReadData():
•
opens the text file and reads each line
•
splits each line into the key and data
•
calls InsertData() with an object containing each key and matching data.
Write program code for ReadData()
Save your program.
Copy and paste the program code into part 3(e) in the evidence document.
[5]
The function GetRecord():
(f)
•
takes an integer key field as a parameter
•
calculates the hash value for the key field using the appropriate function
•
searches the hash table for the record with the matching key field
•
returns the data for the record if the record is found
•
returns "Not found" if the record is not found.
Write program code for GetRecord()
Save your program.
Copy and paste the program code into part 3(f) in the evidence document.
[5]
(g) The main program:
•
calls InitialiseHashTable() and ReadData()
•
takes five integer key fields as input from the user
•
calls GetRecord() with each input and outputs the return value.
(i)
Write program code for the main program.
Save your program.
Copy and paste the program code into part 3(g)(i) in the evidence document.
[3]
(ii)
Test your program with the following five inputs in the order given:
528
1128
1828
1062
39
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot(s) into part 3(g)(ii) in the evidence document.
[2]
Permission to reproduce items where third-party owned material protected by copyright is included has been sought and

3
9618/42/O/N/25 · Question 2Oct/Nov 2025Paper 4226 marks
Loading question…
Question text

2
A program stores 20 unique random integers between 0 and 100 (inclusive) in a 1D array that is
local to the main program.
(a) Write program code to declare the array local to the main program and store 20 unique
random numbers between 0 and 100 (inclusive) in the array.
Save your program as Question2_N25.
Copy and paste the program code into part 2(a) in the evidence document.
[3]
(b) The procedure PrintArray() takes an integer array as a parameter. The procedure outputs
the array contents on a single line with a space between each integer.
Write the program code for PrintArray()
Save your program.
Copy and paste the program code into part 2(b) in the evidence document.
[3]
(c) The function BubbleSort():
•
takes an integer array as a parameter
•
sorts the data into ascending order using a bubble sort
•
returns the sorted array.
The function needs to work for an array of any length.
Do not use an inbuilt sorting method.
Write program code for BubbleSort()
Save your program.
Copy and paste the program code into part 2(c) in the evidence document.
[5]
(d) (i)
The main program:
•
outputs the contents of the array using PrintArray()
•
sorts the array using BubbleSort()
•
outputs "Sorted"
•
outputs the contents of the sorted array using PrintArray()
Write program code for the main program.
Save your program.
Copy and paste the program code into part 2(d)(i) in the evidence document.
[3]
(ii)
Test your program.
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot(s) into part 2(d)(ii) in the evidence document.
[1]
(e) The recursive function RecursiveBinarySearch() takes four parameters:
•
an integer array
•
the lower bound of the array
•
the upper bound of the array
•
the value to find in the array.
The recursive function performs a binary search to find the index of the value in the array.
The function returns the index of the value if it is found. The function returns –1 if the value is
not found.
Write program code for RecursiveBinarySearch()
Save your program.
Copy and paste the program code into part 2(e) in the evidence document.
[6]
(f)
(i)
The main program:
•
prompts the user to enter an integer
•
takes the integer as input
•
calls RecursiveBinarySearch() with the sorted array, appropriate lower bound,
appropriate upper bound and the user’s input as parameters
•
outputs "Not found" if the input is not within the array
•
outputs "Found at position" and the index if the input is within the array.
Write program code to amend the main program.
Save your program.
Copy and paste the program code into part 2(f)(i) in the evidence document.
[3]
(ii)
Test your program three times with each of the inputs described:
Test 1: the smallest number in the array
Test 2: the largest number in the array
Test 3: a number not in the array
Take a screenshot of each output.
Save your program.
Copy and paste the screenshot(s) into part 2(f)(ii) in the evidence document.
[2]

Advertisement
4
9618/42/O/N/25 · Question 3Oct/Nov 2025Paper 4221 marks
Loading question…
Question text

3
A program stores integers in ascending order in an ordered binary tree. The tree is implemented
as a 2D array.
Each node is stored with three values:
•
a pointer to the left node
•
the data
•
a pointer to the right node.
All null values are stored as –1
The binary tree can store up to 50 nodes.
Nodes cannot be deleted from the binary tree.
(a) The binary tree is stored as a global array with the identifier TreeArray. The left pointer, the
data and the right pointer of each node are initialised to –1
The global variable RootPointer stores the index of the root node in the tree, initialised to
–1
The global variable FreeNode stores the index of the next free node in the array, initialised
to 0
Write program code to declare and initialise TreeArray, RootPointer and FreeNode
Save your program as Question3_N25.
Copy and paste the program code into part 3(a) in the evidence document.
[3]
(b) The procedure AddNode():
•
takes an integer to store in the binary tree as a parameter
•
stores the parameter in the next free node in the array
•
finds the position to store the data in the tree by following the appropriate pointers
•
updates the pointer of the new node’s parent node.
The procedure outputs "The tree is full" if the parameter cannot be stored because
the tree is full.
Write program code for AddNode()
Save your program.
Copy and paste the program code into part 3(b) in the evidence document.
[7]
(c) The text file TreeData.txt stores 50 integers. Each integer is on a new line in the file.
The main program reads each integer from the file and stores each integer in the binary tree
in the order they are read.
Write program code for the main program.
Save your program.
Copy and paste the program code into part 3(c) in the evidence document.
[4]
(d) The procedure WriteAllToFile() stores the content of TreeArray in a new text file with
the filename Tree.txt. The file Tree.txt is not provided.
Each node in TreeArray is stored on one line with a comma separating each value.
For example, the current contents of TreeArray are:
Index
0
1
2
0
–1
20
1
1
–1
30
–1
…
49
-1
-1
-1
After writing TreeArray to the text file, Tree.txt will contain:
–1,20,1
–1,30,–1
…
-1,-1,-1
Write program code for WriteAllToFile()
Include exception handling when writing to the file.
Save your program.
Copy and paste the program code into part 3(d) in the evidence document.
[5]
Amend the main program to call WriteAllToFile()
(e) (i)
Save your program.
Copy and paste the program code into part 3(e)(i) in the evidence document.
[1]
(ii)
Test your program.
Take a screenshot that shows all of the content stored in the file Tree.txt
In this screenshot you need to make sure the filename is visible.
Save your program.
Copy and paste the screenshot(s) into part 3(e)(ii) in the evidence document.
[1]
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

5
9618/43/O/N/25 · Question 2Oct/Nov 2025Paper 4324 marks
Loading question…
Question text

2
A program reads string data from a text file into a linear queue and then compresses the data.
The queue is created using the global 1D array Queue. The queue can store up to 100 elements.
Each element is initialised to the empty string ""
The queue has two pointers that are global to the program:
•
QueueHead that points to the index of the first element in the queue, initialised to –1
•
QueueTail that points to the index of the last element in the queue, initialised to –1
The global variable NumberItems stores the number of elements stored in the queue, initialised
to 0
(a) Write program code to declare and initialise Queue, QueueHead, QueueTail and
NumberItems
Save your program as Question2_N25.
Copy and paste the program code into part 2(a) in the evidence document.
[2]
(b) The function Enqueue() takes a string as a parameter. The function checks if the queue is
full, and returns Boolean FALSE if the queue is full.
If the queue is not full, the parameter is inserted into the next position in Queue. The function
updates the appropriate pointer(s), updates NumberItems and then returns Boolean TRUE
Write program code for Enqueue()
Save your program.
Copy and paste the program code into part 2(b) in the evidence document.
[5]
(c) The function Dequeue() returns the string "False" if the queue is empty.
If the queue is not empty, the function returns the next element in the queue. The function
updates the appropriate pointer(s) and updates NumberItems
Write program code for Dequeue()
Save your program.
Copy and paste the program code into part 2(c) in the evidence document.
[3]
(d) The text file BinaryData.txt stores individual binary digits, '1' and '0'. Each digit is on
a new line. For example, the first line in the text file stores '1', the second line stores '1'
The procedure ReadData() reads in each line from the text file BinaryData.txt and
inserts it into the queue using the appropriate method.
The procedure needs to work for a text file with any number of lines up to a maximum of 100.
Write program code for ReadData()
Save your program.
Copy and paste the program code into part 2(d) in the evidence document.
[5]
(e) The string data in the text file is compressed.
The compression algorithm counts the number of times each binary digit appears
consecutively, then stores the binary digit followed by the number of times it appears. The
algorithm stores the compressed data in a single string.
For example, if the text file contains the data:
1
1
0
0
0
1
1
1
The compression algorithm will create the string "120313" because there are two '1' digits,
followed by three '0' digits, followed by three '1' digits.
The procedure Compress() uses Dequeue() to remove each element from the queue in
turn. The procedure then compresses the data following the compression algorithm described.
The new compressed string is stored in the global variable NewString
You can assume that one binary digit will never appear more than nine times consecutively in
the sequence.
You can assume that there will always be at least one item in the queue.
Write program code for Compress()
Save your program.
Copy and paste the program code into part 2(e) in the evidence document.
[6]
(f)
(i)
Write program code for the main program to:
•
call ReadData()
•
call Compress()
•
output the content of the compressed string.
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(s) into part 2(f)(ii) in the evidence document.
[1]

6
9618/43/O/N/25 · Question 3Oct/Nov 2025Paper 4320 marks
Loading question…
Question text

3
A program is written to perform different individual processes using arrays.
(a) A 1D array stores 10 integers.
A recursive function RecursiveCount() takes three parameters:
(i)
•
ArrayCopy, an array of integers
•
NumberElements, the number of elements in the array of integers
•
DataToFind, an integer data to find in the array.
The function counts and returns the number of times DataToFind is in ArrayCopy
The recursive algorithm:
•
returns 0 if there are no elements in ArrayCopy
•
compares the first element in ArrayCopy with DataToFind
◦
if the element matches, the function returns 1 added to the return value from a
recursive call (passing the array without the first element)
◦
if the element does not match, the function returns the return value from a
recursive call (passing the array without the first element).
Write program code for RecursiveCount()
Save your program as Question3_N25.
Copy and paste the program code into part 3(a)(i) in the evidence document.
[6]
(ii)
The main program stores the following data in a 1D array of integers in the order given:
0
5
1
2
5
9
9
6
5
0
The main program calls RecursiveCount() with the parameters:
•
0 as the data to find
•
10 as the number of elements
•
the array of 10 integers.
The return value is output.
Write program code for the main program.
Save your program.
Copy and paste the program code into part 3(a)(ii) in the evidence document.
[3]
(iii)
Test your program.
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot(s) into part 3(a)(iii) in the evidence document.
[1]
(b) The program stores the following string. The string has four statements that are each
terminated by a semi-colon ';'
"x=0;y=1;x=x+y;y++;"
Write program code to amend the main program to store the string "x=0;y=1;x=x+y;y++;"
(i)
in a local variable.
Save your program.
Copy and paste the program code into part 3(b)(i) in the evidence document.
[1]
The function SplitData() splits a string parameter into four individual lines of code.
(ii)
The function creates an array of the strings where each line is stored in a new array
element without the semi-colon.
The function returns the array of strings.
Do not use an inbuilt function to split the string.
Write program code for SplitData()
Save your program.
Copy and paste the program code into part 3(b)(ii) in the evidence document.
[6]
The main program needs to call SplitData() with the string from part 3(b)(i). The
(iii)
main program then needs to output each element from the returned array on a new line.
Write program code to amend the main program.
Save your program.
Copy and paste the program code into part 3(b)(iii) in the evidence document.
[2]
(iv)
Test your program.
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot(s) into part 3(b)(iv) in the evidence document.
[1]
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

7
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]

8
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

Advertisement
9
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/

10
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

11
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]

12
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

13
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
14
9618/42/O/N/23 · Question 2Oct/Nov 2023Paper 4217 marks
Loading question…
Question text

2
An integer is said to be divisible by another integer if the result of the division is also an integer.
For example:
10 is divisible by 1, 2, 5 and 10:
•
10 ÷ 1 = 10
•
10 ÷ 2 = 5
•
10 ÷ 5 = 2
•
10 ÷ 10 = 1
10 is not divisible by 4:
•
10 ÷ 4 = 2.5
1, 2, 5 and 10 are said to be the divisors of 10.
The iterative function IterativeCalculate() totals all the divisors of its integer parameter and
returns this total.
Example 1: if the parameter is 10, the total will be 18 (1 + 2 + 5 + 10) .
Example 2: if the parameter is 4, the total will be 7 (1 + 2 + 4) .
A pseudocode algorithm for IterativeCalculate() is shown.
FUNCTION IterativeCalculate(Number : INTEGER) RETURNS INTEGER
DECLARE Total : Integer
DECLARE ToFind : Integer
← Number
ToFind
Total ← 0
WHILE Number <> 0
IF ToFind MODULUS Number = 0 THEN
← Total + Number
Total
ENDIF
← Number - 1
Number
ENDWHILE
RETURN Total
ENDFUNCTION
The operator MODULUS calculates the remainder when one number is divided by another.
(a) (i)
Write program code for IterativeCalculate().
Save your program as Question2_N23.
Copy and paste the program code into part 2(a)(i) in the evidence document.
[5]
(ii)
Write program code to call IterativeCalculate() with 10 as the parameter and
output the return value.
Save your program.
Copy and paste the program code into part 2(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 2(a)(iii) in the evidence document.
[1]
(b) IterativeCalculate() has been rewritten as the recursive function RecursiveValue().
A pseudocode algorithm for RecursiveValue() is given. The function is incomplete.
FUNCTION RecursiveValue(Number : INTEGER, ToFind : INTEGER)
RETURNS INTEGER
IF Number = … THEN
RETURN 0
ELSE
IF ToFind … Number = 0 THEN
RETURN … + RecursiveValue(Number - 1, ToFind)
ELSE
… RecursiveValue(Number - 1, … )
ENDIF
ENDIF
ENDFUNCTION
(i)
Write program code for RecursiveValue().
Save your program.
Copy and paste the program code into part 2(b)(i) in the evidence document.
[7]
(ii)
Write program code to call RecursiveValue() with 50 as the first parameter and 50
as the second parameter and output the return value.
Save your program.
Copy and paste the program code into part 2(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 2(b)(iii) in the evidence document.
[1]

15
9618/41/O/N/22 · Question 1Oct/Nov 2022Paper 4123 marks
Loading question…
Question text

1
The text file IntegerData.txt stores 100 integer numbers between 1 and 100 inclusive. A
program is required to read in this data and perform searching and sorting on the data.
(a) Write program code to declare a global 1D array, DataArray, with space for 100 integer
values.
Save your program as Question1_N22.
Copy and paste the program code into part 1(a) in the evidence document.
[2]
(b) The procedure ReadFile() must read in the numbers from the text file and store each one
in the array. Use appropriate exception handling.
Write program code for the procedure ReadFile().
Save your program.
Copy and paste the program code into part 1(b) in the evidence document.
[6]
(c) The function FindValues() asks the user to enter a number to search for in the array.
The number input must be a whole number between 1 and 100 inclusive. The function then
returns the number of times the number input appears in the array.
Write program code for the function FindValues().
Save your program.
Copy and paste the program code into part 1(c) in the evidence document.
[7]
(d) (i)
Write program code to call ReadFile() and FindValues() from the main program.
The return value from FindValues() must be output with an appropriate message.
Save your program.
Copy and paste the program code into part 1(d)(i) in the evidence document.
[3]
(ii)
Test your program using the number 61 as input.
Take a screenshot to show the output.
Copy and paste the screenshot into part 1(d)(ii) in the evidence document.
[1]
(e) The procedure BubbleSort() needs to perform a bubble sort on the array and print the
contents of the sorted array.
Write program code for the procedure BubbleSort() and call it from the main program.
Save your program.
Copy and paste the program code into part 1(e) in the evidence document.
[4]

Advertisement