Data types, records, arrays and abstract data types – A Level Computer Science (9618)
50 questions · page 1 of 4 · Clear filters
Question text
1
A program stores integer data in a linear queue.
The queue is created as a global 1D array with space for 10 elements.
The queue has a global head pointer, initialised to –1, that points to the first element in
the queue.
The queue has a global tail pointer, initialised to –1, that points to the last element in the queue.
(a) The main program initialises the 10 elements of the queue to –1 and initialises the
two pointers.
Write program code to declare the global variables and initialise the queue and its pointers.
Save your program as Question1_J26.
Copy and paste the program code into 1(a) in the evidence document.
[3]
(b) The function Enqueue() performs these tasks:
•
takes an integer parameter
•
checks if the queue is full
•
returns Boolean FALSE, if the queue is full
•
stores the parameter in the queue, updates the appropriate pointer(s) and returns
Boolean TRUE, if the queue is not full.
Write program code for Enqueue()
Save your program.
Copy and paste the program code into 1(b) in the evidence document.
[7]
© Cambridge University Press & Assessment 2026
(c) (i)
Write program code to extend the main program to:
•
take 11 integers as input from the user
•
use the appropriate function to insert each integer into the queue
•
output a message if at any point the queue is full
•
output the final content of the queue array. Do not dequeue the data.
You do not need to validate the data input.
Save your program.
Copy and paste the program code into 1(c)(i) in the evidence document.
[5]
(ii)
Test your program.
Enter the following data as input in the order given:
1
2
3
4
5
6
7
8
9
10
11
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into 1(c)(ii) in the evidence document.
[1]
© Cambridge University Press & Assessment 2026
(d) The function Dequeue():
•
returns –1 if the queue is empty
•
updates the pointer(s) and returns the next item from the queue if the queue is
not empty.
Write program code for Dequeue()
Save your program.
Copy and paste the program code into 1(d) in the evidence document.
[6]
(e) (i)
Write program code to amend the main program to:
•
take 6 integers as input from the user and store them in the queue using the
appropriate function
•
use the appropriate function to remove 4 elements from the queue
•
output each removed element from the queue.
Save your program.
Copy and paste the program code into 1(e)(i) in the evidence document.
[3]
(ii)
Test your program.
Enter the following data as input in the order given:
100
10
5
6
18
68
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into 1(e)(ii) in the evidence document.
[1]
© Cambridge University Press & Assessment 2026
Question text
2
A program stores data about toys that are made from bricks. The toys are stored in a record
structure with four fields:
•
ToyName: the string name of the toy
•
NumberBricks: the integer number of bricks in the toy
•
Difficulty: the string difficulty level of the toy (easy, medium or expert)
•
Category: the string category of the toy (for example town, display, play).
(a) Write program code to declare a record structure with the identifier ToyRecord and the
four fields.
Save your program as Question2_J26.
Copy and paste the program code into 2(a) in the evidence document.
[2]
(b) The text file Toys.txt stores data about 16 toys. Each toy is on a new line in the file. The
data is stored in the order: toy name, number of bricks, difficulty, category. Each value is
separated by a comma.
For example, the first line in the file is:
Fire Station,3981,Expert,Town
The name of the toy is Fire Station. The toy has 3981 bricks. The difficulty level is Expert.
The category is Town.
The function ReadData():
(i)
•
takes an array of type ToyRecord as a parameter
•
opens the file Toys.txt and reads in the data
•
creates a new record of type ToyRecord for each toy read from the file
•
stores each record in the parameter array
•
returns the populated array.
The function uses exception handling when opening and reading from the file.
Write program code for ReadData()
Save your program.
Copy and paste the program code into 2(b)(i) in the evidence document.
[8]
© Cambridge University Press & Assessment 2026
The procedure OutputToys() takes an array of type ToyRecord as a parameter and
(ii)
outputs the data for each toy in the array.
Write program code for OutputToys()
Save your program.
Copy and paste the program code into 2(b)(ii) in the evidence document.
[3]
(iii)
Write program code for the main program to:
•
declare an array of type ToyRecord with space for 16 elements
•
call the function ReadData() with the empty array and store the returned array
•
call OutputToys() with the returned array.
Save your program.
Copy and paste the program code into 2(b)(iii) in the evidence document.
[3]
(iv)
Test your program.
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into 2(b)(iv) in the evidence document.
[1]
(c) The program needs to allow a user to enter the difficulty level of a toy and then write all toys
that match that difficulty level to an external text file.
The function SearchToys():
(i)
•
takes an array of type ToyRecord as a parameter
•
prompts the user to enter the difficulty level
•
takes the difficulty level as input until it is valid
•
creates a new array containing the ToyRecord objects that match the difficulty
level input
•
returns the populated array.
Write program code for SearchToys()
Save your program.
Copy and paste the program code into 2(c)(i) in the evidence document.
[5]
© Cambridge University Press & Assessment 2026
The procedure WriteToys():
(ii)
•
takes an array of type ToyRecord as a parameter
•
creates and opens the file SelectedToys.txt to write
•
writes each record from ToyRecord to SelectedToys.txt with each field
separated by a comma and each record on a new line. For example:
<ToyName>,<NumberBricks>,<Difficulty>,<Category>
Write program code for WriteToys()
Save your program.
Copy and paste the program code into 2(c)(ii) in the evidence document.
[3]
(iii)
Write program code to extend the main program to:
•
call SearchToys() with the populated array and store the return array
•
call WriteToys() with the returned array.
Save your program.
Copy and paste the program code into 2(c)(iii) in the evidence document.
[2]
(iv)
Test your program. Enter the appropriate input for a medium difficulty level
when prompted.
Take one screenshot of the text file the program produces with the contents and
filename clearly visible.
Save your program.
Copy and paste the screenshot into 2(c)(iv) in the evidence document.
[1]
© Cambridge University
Question text
A program stores nine lower‑case strings in a 1D array TheData. The following strings are
1
stored in the order shown:
laptop
tablet
printer
computer
processor
monitor
memory
cable
touchscreen
(a) Write program code to declare the array TheData in the main program. Store the given
strings in the array.
Save your program as Question1_J26.
Copy and paste the program code into 1(a) in the evidence document.
[2]
The procedure FindLongestShortest():
(b) (i)
•
takes an array of strings as a parameter
•
finds and outputs the longest string in the array of strings
•
finds and outputs the shortest string in the array of strings.
All outputs must have appropriate messages. Do not use in‑built functions to find the
longest and shortest string in the array. You may use an in‑built length function to find
the length of a string.
You can assume that there will only be one string that has the shortest length.
You can assume that there will only be one string that has the longest length.
Write program code for FindLongestShortest()
Save your program.
Copy and paste the program code into 1(b)(i) in the evidence document.
[5]
© Cambridge University Press & Assessment 2026
Write program code to extend the main program to call FindLongestShortest()
(ii)
with TheData
Save your program.
Copy and paste the program code into 1(b)(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 1(b)(iii) in the evidence document.
[1]
The function InsertionSort():
(c) (i)
•
takes an array of strings as a parameter
•
sorts the strings in the array into ascending alphabetical order using an insertion sort
•
returns the sorted array.
The function must work for an array of any length. Do not use an in‑built function to sort
the array.
Write program code for InsertionSort()
Save your program.
Copy and paste the program code into 1(c)(i) in the evidence document.
[5]
(ii)
Write program code to extend the main program to perform the following in the
order shown:
•
output the string "Before sorting"
•
output the contents of the array TheData
•
call InsertionSort() with TheData as a parameter
•
output the string "After sorting"
•
output the contents of the returned sorted array.
Save your program.
Copy and paste the program code into 1(c)(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 into 1(c)(iii) in the evidence document.
[1]
© Cambridge University Press & Assessment 2026
Question text
A 2D array is used to store the linked list, LinkedList. The linked list stores a maximum of
3
eight data items. Each data item is an integer.
The array elements not currently used in the linked list are stored as the empty list.
LinkedList has two pointers:
•
StartLinkedList stores a pointer to the first node in the linked list
•
StartEmptyList stores a pointer to the first node in the empty list.
A data value of -1 indicates there is no data in the node.
A pointer of -1 indicates it is the end of the linked list, or the end of the empty list.
The linked list and pointers all have global scope.
The linked list already has the following data stored:
array index
data
pointer
0
9512
5
1
5541
3
2
1999
0
3
6651
–1
4
–1
6
5
5612
1
6
–1
–1
7
–1
4
StartLinkedList currently stores 2
StartEmptyList currently stores 7
(a) Write program code to declare and initialise the linked list and the pointers.
Save your program as Question3_J26.
Copy and paste the program code into 3(a) in the evidence document.
[5]
© Cambridge University Press & Assessment 2026
The procedure PrintListItems() outputs the data in each node of the linked list.
(b) (i)
The procedure starts with the first node in the linked list and follows the pointers until it
has reached the end of the linked list.
Write program code for PrintListItems()
Save your program.
Copy and paste the program code into 3(b)(i) in the evidence document.
[4]
The procedure PrintEmptyList() outputs the index of each node in the empty list.
(ii)
The procedure starts with the first node in the empty list and follows the pointers until it
has output the index of each node in the empty list.
Write program code for PrintEmptyList()
Save your program.
Copy and paste the program code into 3(b)(ii) in the evidence document.
[3]
(iii)
Write program code to extend the main program to:
•
output "Linked list" and then call PrintListItems()
•
output "Empty list" and then call PrintEmptyList()
Save your program.
Copy and paste the program code into 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 into 3(b)(iv) in the evidence document.
[2]
© Cambridge University Press & Assessment 2026
(c) New nodes are inserted at the end of the linked list.
The procedure AddItem() takes an integer parameter, inserts the parameter in the space
identified by StartEmptyList and updates the appropriate pointer(s).
If there is no space in the linked list, the procedure outputs "List is full"
Write program code for AddItem()
(i)
Save your program.
Copy and paste the program code into 3(c)(i) in the evidence document.
[8]
(ii)
Write program code to extend the main program to:
•
use AddItem() to insert a new node into the linked list with the data 8451
•
output the new contents of the linked list using PrintListItems() with a
suitable heading
•
output the new contents of the empty list using PrintEmptyList() with a
suitable heading.
Save your program.
Copy and paste the program code into 3(c)(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 3(c)(iii) in the evidence document.
[1]
© Cambridge University Press & Assessment 2026
Permission to reproduce items where third‑party owned material protected by copyright is included has been sought and cleared where possible. Every
reasonable effort has been made by the publisher (Cambridge University Press & Assessment) to trace copyright holders, but if any items requiring clearance
have unwittingly been included, the 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 our Copyright
Acknowledgements Booklet. This is produced for each series of examinations and is freely
Question text
A program stores 15 strings in the 1D array DataArray
1
(a) Write program code to declare the global 1D array DataArray with space for 15
string elements.
Save your program as Question1_J26.
Copy and paste the program code into 1(a) in the evidence document.
[1]
(b) The procedure CreateArray() takes 15 strings as input from the user. Each input is
converted to lower case and stored in DataArray in the order input.
Write program code for CreateArray()
Save your program.
Copy and paste the program code into 1(b) in the evidence document.
[4]
(c) The procedure OutputArray() outputs the data in each element in DataArray on one
line with a space between each element.
Write program code for OutputArray()
Save your program.
Copy and paste the program code into 1(c) in the evidence document.
[2]
© Cambridge University Press & Assessment 2026
Write program code for the main program to call CreateArray() and
(d) (i)
then OutputArray()
Save your program.
Copy and paste the program code into 1(d)(i) in the evidence document.
[1]
(ii)
Test your program. Enter the following data for the inputs in the order given:
1
One
2
TWO
three
??
house
99987
popping
green
eleven
12
Twenty
THIRTY
16
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into 1(d)(ii) in the evidence document.
[1]
© Cambridge University Press & Assessment 2026
(e) The procedure FindWords() takes a string as input from the user and converts it to
lower case. The procedure counts and outputs the number of times the input is stored
in DataArray
Write program code for FindWords(). Do not use an in‑built function to count the number
of times the input is stored in DataArray
Save your program.
Copy and paste the program code into 1(e) in the evidence document.
[5]
The procedure SortLength() uses a bubble sort to order the elements in DataArray
(f)
(i)
into ascending order by the length of each string.
The bubble sort needs to be efficient and stop iterating as soon as it identifies that
DataArray is in the correct order.
Write program code for SortLength(). Do not use an in‑built function to sort the array
or check if the array is in the correct order.
Save your program.
Copy and paste the program code into 1(f)(i) in the evidence document.
[5]
Write program code to extend the main program by calling FindWords() then
(ii)
SortLength() then OutputArray()
Save your program.
Copy and paste the program code into 1(f)(ii) in the evidence document.
[1]
© Cambridge University Press & Assessment 2026
(iii)
Test your program. Enter the following data to be stored in the array in the order given:
one
One
1
three
three
One
house
99987
popping
green
eleven
One
Twenty
THIRTY
One
Enter "one" as the string to find in the array.
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into 1(f)(iii) in the evidence document.
[1]
© Cambridge University Press & Assessment 2026
Question text
3
A binary tree is designed to store up to 100 positive integers. The binary tree stores data in
ascending numerical order.
The data in the binary tree is created as a global 2D array with the identifier BinaryTree
Each node has three values:
•
a pointer to the array index with the left node
•
the integer data
•
a pointer to the array index with the right node.
A null pointer is represented by the integer –1. All nodes are initialised with the null data –1
The table has example data for a binary tree:
index
left pointer
data
right pointer
0
1
20
2
1
4
10
–1
2
3
26
–1
3
–1
22
–1
4
–1
8
–1
The binary tree has two global pointers:
•
TreeRootPointer, initialised to –1, stores the index of the root node of the binary tree
•
FirstFreeNodePointer, initialised to 0, stores the index of the first node that does not
store any data.
Data cannot be removed or replaced from the tree. FirstFreeNodePointer is incremented
each time a node is added to the binary tree.
(a) The main program initialises all 100 nodes, TreeRootPointer
and FirstFreeNodePointer
Write program code for the main program.
Save your program as Question3_J26.
Copy and paste the program code into 3(a) in the evidence document.
[3]
© Cambridge University Press & Assessment 2026
(b) The procedure AddNode():
•
takes an integer parameter to be stored as the data in a new node
•
stores the data in the first element and updates the root pointer when the tree is empty
•
outputs "The tree is full" if the tree is full
•
stores the data in the next free node if the tree is not full, then:
◦
checks if the data is less than or greater than the root node
◦
follows the left or right pointer(s) repeatedly until it reaches the appropriate
leaf node
◦
updates the appropriate parent node pointer to the new node.
Write program code for AddNode()
Save your program.
Copy and paste the program code into 3(b) in the evidence document.
[7]
(c) The procedure OutputArray() outputs the content of the first 10 elements in the binary
tree. Each node is output on one line in the format:
<left pointer> <data> <right pointer>
For example:
1
20
4
Write program code for OutputArray()
Save your program.
Copy and paste the program code into 3(c) in the evidence document.
[2]
© Cambridge University Press & Assessment 2026
(d) (i)
Write program code to extend the main program to:
•
take 10 integers as input
•
store each input in the binary tree in the order they are entered using the
appropriate procedure
•
output the content of the binary tree using OutputArray()
Save your program.
Copy and paste the program code into 3(d)(i) in the evidence document.
[3]
(ii)
Test your program. Enter the following numbers in the order shown:
50
26
120
236
2
16
67
49
165
15
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into 3(d)(ii) in the evidence document.
[1]
© Cambridge University Press & Assessment 2026
(e) An in‑order tree traversal outputs the contents of the binary tree in ascending numerical
order by:
•
visiting the left node
•
output the data
•
visiting the right node.
For example, an in‑order traversal on the following binary tree will output: 5 12 18 20 26
20
12
26
5
18
The recursive procedure InOrder():
(i)
•
takes the root pointer as a parameter
•
makes a recursive call with the left pointer as the parameter if the left pointer is
not null
•
outputs the data at the parameter
•
makes a recursive call with the right pointer as the parameter if the right pointer is
not null.
Write program code for InOrder()
Save your program.
Copy and paste the program code into 3(e)(i) in the evidence document.
[7]
Write program code to extend the main program to call InOrder()
(ii)
Save your program.
Copy and paste the program code into 3(e)(ii) in the evidence document.
[1]
© Cambridge University Press & Assessment 2026
(iii)
Test your program. Enter the following numbers in the order given:
50
26
120
236
2
16
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]
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
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]
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
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]
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
Question text
1
A program stores positive integers in a circular queue.
The queue is stored as a global 1D array of 20 integers with the identifier Queue. Each index is
initialised with the data –1
The global variable HeadPointer, initialised to –1, points to the first element in the queue.
The global variable TailPointer, initialised to –1, points to the last element in the queue.
The global variable NumberItems, initialised to 0, stores the number of items in the queue.
(a) Write program code to declare and initialise Queue, HeadPointer, TailPointer and
NumberItems
Save your program as Question1_J25.
Copy and paste the program code into part 1(a) in the evidence document.
[2]
(b) The function Enqueue():
•
takes an integer as a parameter
•
checks if the queue is full
•
returns FALSE if the queue is full
•
stores the parameter in the next position in the queue and returns TRUE if the queue is
not full
•
updates the appropriate pointers and NumberItems
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 main program:
•
attempts to store each of the integers 1 to 25 (inclusive) in the queue in ascending
numerical order using Enqueue()
•
outputs the integer that was passed to Enqueue() and "Successful" if it was stored
in the queue, or "Unsuccessful" if it was not stored in the queue.
For example:
if the integer 5 is passed to Enqueue() and is stored in the queue, the output will
be: "5 Successful"
if the integer 23 is passed to Enqueue() and is not stored in the queue, the output
will be: "23 Unsuccessful"
Write program code for the main program.
Save your program.
Copy and paste the program code into part 1(c) in the evidence document.
[3]
(d) The function Dequeue() returns –1 if the queue is empty. If the queue is not empty, the
function returns the next item in the queue, updates the appropriate pointers and updates
NumberItems
Write program code for Dequeue()
Save your program.
Copy and paste the program code into part 1(d) in the evidence document.
[6]
Write program code to extend the main program to call Dequeue() twice and output the
(e) (i)
return value each time.
Save your program.
Copy and paste the program code into part 1(e)(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 1(e)(ii) in the evidence document.
[1]
Question text
2
A program reads data from a text file, splits the data depending on its content and stores the
separated data into different files.
The text file TheData.txt contains 72 lines of data. Each line of data has an integer number and
a string colour that are separated by a comma. For example, the first line in the file is:
10,red
The integer is 10 and the string is "red"
The file contains six different colours: red, green, blue, orange, yellow, pink.
(a) The function ReadData():
•
prompts the user to enter a filename and reads this filename from the user
•
opens the file and reads each line of data into a 1D array
•
returns the populated 1D array.
The function needs to work for a file that contains an unknown number of lines.
Write program code for ReadData()
Save your program as Question2_J25.
Copy and paste the program code into part 2(a) in the evidence document.
[7]
(b) The procedure SplitData() takes a 1D string array as a parameter with the identifier
DataArray
The procedure declares six 1D arrays: one array for each colour that appears in the file (red,
green, blue, orange, yellow, pink).
The procedure accesses each string in DataArray. The data in each string is split into the
integer and the colour. The integer is stored in the array that matches the colour.
For example, the first string in DataArray has the integer 10 and the colour red, so the
integer 10 is stored in the array for the colour red.
Write program code for SplitData()
Save your program.
Copy and paste the program code into part 2(b) in the evidence document.
[6]
(c) The procedure StoreData():
•
takes two parameters: a 1D array DataToStore and a filename
•
opens the text file with the filename that is passed as a parameter
•
appends each item of data from DataToStore to a new line in the text file
•
uses exception handling when opening and writing data to the text file.
Write program code for StoreData()
Save your program.
Copy and paste the program code into part 2(c) in the evidence document.
[5]
(d) Each of the six colours has a blank text file where the numbers will be stored. The names of
these six text files are:
•
Blue.txt
•
Green.txt
•
Orange.txt
•
Pink.txt
•
Red.txt
•
Yellow.txt
The procedure SplitData() needs amending to call StoreData() six times, with each of
the six colour arrays and the name of the text file that corresponds to that colour.
For example, StoreData() will be called with the red array and the file name "Red.txt"
Write program code to amend SplitData()
Save your program.
Copy and paste the program code into part 2(d) in the evidence document.
[2]
(e) The main program calls ReadData() and then SplitData()
(i)
Write program code for the main program.
Save your program.
Copy and paste the program code into part 2(e)(i) in the evidence document.
[3]
Test your program. Input the text "TheData.txt" when prompted.
(ii)
Take a screenshot of the output(s) and a screenshot showing the content of the file that
stores the red numbers.
Save your program.
Copy and paste the screenshot(s) into part 2(e)(ii) in the evidence document.
[2]
Question text
A program reads data from a text file and stores it in a stack. The stack Stack is stored as a 1D
1
array of up to 20 elements. The pointer TopOfStack stores the index of the last element stored in
the stack.
(a) Stack is a global array of strings with all elements initialised to "-1"
TopOfStack is a global variable initialised to –1
Write program code to declare and initialise Stack and TopOfStack
Save your program as Question1_J25.
Copy and paste the program code into part 1(a) in the evidence document.
[2]
(b) The function Push() takes a string parameter and attempts to store it on the stack.
The function returns –1 if the stack is full.
The function returns 1 if the parameter is successfully pushed onto the stack.
Write 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 item from the stack.
The function returns "–1" if the stack is empty.
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 text file StackData.txt stores numbers and mathematical operators. Each number
and operator are on a new line in the text file.
The mathematical operators used in this program are:
mathematical
meaning
operator
+
addition
–
subtraction
/
division
*
multiplication
^
power of
The procedure ReadData():
•
takes a string filename as a parameter
•
opens the file and reads in each line of data
•
uses the Push() function to insert each line of data onto the stack
•
outputs "Stack full" if any data cannot be stored on the stack because the stack is
full
•
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 ReadData()
Save your program.
Copy and paste the program code into part 1(d) in the evidence document.
[6]
(e) The values stored in the stack are used to perform mathematical operations.
A total is initialised to the first value in the stack. The first value in the stack will always be a
number.
The next value will be a mathematical operator and the next value will be the number to apply
to the calculation. This is repeated until there are no values left in the stack. There will always
be a number following a mathematical operator.
For example, the contents of a stack are:
TopOfStack
10
+
3
–
2
The total is initialised to the first value in the stack: 10
total = 10
The next two values are: + 3
total = 10 + 3 = 13
The next two values are: – 2
total = 13 – 2 = 11
The final total is 11 and this is returned.
Write program code for the function Calculate() to:
•
take each value from the stack
•
calculate and return the final total.
Save your program.
Copy and paste the program code into part 1(e) in the evidence document.
[7]
(f)
(i)
Write program code to extend the main program to:
•
take a filename as input from the user
•
call ReadData() with the input filename
•
call Calculate()
•
output the final total.
Save your program.
Copy and paste the program code into part 1(f)(i) in the evidence document.
[2]
(ii)
Test your program twice with the file names:
StackData.txt
SecondStack.txt
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 1(f)(ii) in the evidence document.
[2]