Data types, records, arrays and abstract data types – A Level Computer Science (9618)
50 questions · page 3 of 4 · Clear filters
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]
Question text
1
A program reads data from a file and searches for specific data.
(a) The main program needs to read 25 integer data items from the text file Data.txt into a
local 1D array, DataArray
(i)
Write program code to declare the local array DataArray
Save your program as Question1_J2023.
Copy and paste the program code into part 1(a)(i) in the evidence document.
[1]
(ii)
Amend the main program to read the contents of Data.txt into DataArray
Save your program.
Copy and paste the program code into part 1(a)(ii) in the evidence document.
[4]
(b) (i)
The procedure PrintArray() takes an integer array as a parameter and outputs the
contents of the array in the order they are stored.
The items are printed on the same line, for example:
10
4
5
13
25
Write program code for the procedure PrintArray()
Save your program.
Copy and paste the program code into part 1(b)(i) in the evidence document.
[3]
(ii)
Amend the main program to output the contents of DataArray using the procedure
PrintArray()
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]
(c) The function LinearSearch():
•
takes an integer array and integer search value as parameters
•
counts and returns the number of times the search value is found in the array.
Write program code for the function LinearSearch()
Save your program.
Copy and paste the program code into part 1(c) in the evidence document.
[3]
(d) (i)
Amend the main program to:
•
prompt the user to input a whole number between 0 and 100 inclusive
•
read and validate the input from the user
•
call LinearSearch() with DataArray and the validated input value
•
output the result in the format:
The number 7 is found 2 times.
Save your program.
Copy and paste the program code into part 1(d)(i) in the evidence document.
[4]
(ii)
Test your program by inputting the number 12.
Take a screenshot of the output.
Save your program.
Copy and paste the screenshot into part 1(d)(ii) in the evidence document.
[1]
Question text
3
A program implements two stacks using 1D arrays. One stack stores the names of colours. One
stack stores the names of animals.
(a) The program contains the following global arrays and variables:
•
1D array Animal to store the names of up to 20 animals.
•
1D array Colour to store the names of up to 10 colours.
•
AnimalTopPointer to point to the next free space in the array Animal, initialised to 0.
•
ColourTopPointer to point to the next free space in the array Colour, initialised to 0.
Write program code to declare the global arrays and variables.
Save your program as Question3_J2023.
Copy and paste the program code into part 3(a) in the evidence document.
[3]
(b) (i)
Study the pseudocode function PushAnimal():
FUNCTION PushAnimal(DataToPush : STRING) RETURNS BOOLEAN
IF AnimalTopPointer = 20 THEN
RETURN FALSE
ELSE
DataToPush
Animal[AnimalTopPointer]
AnimalTopPointer + 1
AnimalTopPointer
RETURN TRUE
ENDIF
ENDFUNCTION
Write program code for the function PushAnimal()
Save your program.
Copy and paste the program code into part 3(b)(i) in the evidence document.
[3]
(ii)
Study the pseudocode function PopAnimal():
FUNCTION PopAnimal() RETURNS STRING
DECLARE ReturnData : STRING
IF AnimalTopPointer = 0 THEN
RETURN ""
ELSE
Animal[AnimalTopPointer - 1]
ReturnData
AnimalTopPointer - 1
AnimalTopPointer
RETURN ReturnData
ENDIF
ENDFUNCTION
Write program code to declare the function PopAnimal()
Save your program.
Copy and paste the program code into part 3(b)(ii) in the evidence document.
[3]
(iii)
The procedure ReadData():
•
reads the animal names from the file AnimalData.txt
•
uses PushAnimal() to insert each name onto the stack
•
uses appropriate exception handling if the file does not exist.
Write program code for the procedure ReadData()
Save your program.
Copy and paste the program code into part 3(b)(iii) in the evidence document.
[5]
(iv)
The function PushColour() performs the same actions as PushAnimal() but inserts
an item into Colour.
The function PopColour() performs the same actions as PopAnimal() but removes
the next item from Colour.
Write program code for the functions PushColour() and PopColour()
Save your program.
Copy and paste the program code into part 3(b)(iv) in the evidence document.
[2]
(v)
Amend the procedure ReadData() so that it also:
•
reads the colours from the text file ColourData.txt
•
uses PushColour() to insert each colour onto the stack
•
uses appropriate exception handling if the file does not exist.
Save your program.
Copy and paste the program code into part 3(b)(v) in the evidence document.
[2]
(c) The procedure OutputItem():
•
pops the next item from both Animal and Colour
•
outputs the colour and animal on one line, for example "black horse"
If there is no data in Colour:
•
the animal is pushed back onto Animal
•
"No colour" is output.
If there is no data in Animal:
•
the colour is pushed back onto Colour
•
"No animal" is output.
Write program code for the procedure OutputItem()
Save your program.
Copy and paste the program code into part 3(c) in the evidence document.
[5]
(d) The main program:
•
calls the procedure ReadData()
•
calls OutputItem() four times.
(i)
Write program code for the main program.
Save your program.
Copy and paste the program code into part 3(d)(i) in the evidence document.
[1]
(ii)
Test your program.
Take a screenshot of the output.
Save your program.
Copy and paste the screenshot into part 3(d)(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 examinat
Question text
1
A 1D array needs to store the names of 10 animals.
(a) Write program code to declare the global string array Animals to store 10 items.
Save your program as Question1_J2023.
Copy and paste the program code into part 1(a) in the evidence document.
[2]
(b) The main program needs to store the following animals in the array:
horse
lion
rabbit
mouse
bird
deer
whale
elephant
kangaroo
tiger
Write program code to store these animal names in the array.
The names must be in lower case and stored in the order given in the list.
Save your program.
Copy and paste the program code into part 1(b) in the evidence document.
[2]
(c) The following pseudocode procedure sorts the array into a descending alphabetical order
using only the first character in each animal name.
The function LENGTH(DataArray) returns the number of elements in the array DataArray.
The function MID(String, Start, Quantity) returns Quantity number of characters
from String starting at index Start. The first character in the string is index 0, for example:
MID("tiger", 0, 2) will return "ti"
There are four incomplete statements in the procedure.
PROCEDURE SortDescending()
DECLARE ArrayLength : INTEGER
DECLARE Temp : STRING
LENGTH(Animals)
ArrayLength
0 TO ArrayLength - 1
FOR X
… TO ArrayLength - X - 1
FOR Y
IF MID(Animals[Y], 0, 1) < MID(Animals[ … ], 0, 1) THEN
Animals[ … ]
Temp
Animals[Y + 1]
Animals[Y]
…
Animals[Y + 1]
ENDIF
NEXT Y
NEXT X
ENDPROCEDURE
Write program code for the procedure SortDescending().
Save your program.
Copy and paste the program code into part 1(c) in the evidence document.
[6]
(d) (i)
Write program code to amend the main program to:
•
call the procedure SortDescending()
•
output the sorted contents of the array with each animal name on a new line.
Save your program.
Copy and paste the program code into part 1(d)(i) in the evidence document.
[3]
(ii)
Test your program.
Take a screenshot of the output.
Save your program.
Copy and paste the screenshot into part 1(d)(ii) in the evidence document.
[1]
Question text
2
A business sells a single product. Customers can purchase one or more of this product.
Each sale has an ID and a quantity, for example "ABC" and 2
The business needs a program to store the data about the sales in a circular queue data structure.
(a) Write program code to declare a record structure, SaleData, to store the data about each
sale.
Save your program as Question2_J2023.
Copy and paste the program code into part 2(a) in the evidence document.
[2]
(b) Write program code to:
•
declare a global array, CircularQueue, of 5 items to store the sale records
•
declare the global pointers Head and Tail
•
declare the global variable NumberOfItems
•
initialise all elements of the array CircularQueue to an empty record, where the ID is
null ("") and quantity is -1
•
initialise Head, Tail and NumberOfItems to 0
Save your program.
Copy and paste the program code into part 2(b) in the evidence document.
[4]
(c) The function Enqueue():
•
takes a new record as a parameter
•
inserts the record in the circular queue at the element pointed to by Tail
•
updates pointers and other variables as required
•
returns −1 if the circular queue is full
•
returns 1 if the record is stored successfully.
Write program code for the function Enqueue().
Save your program.
Copy and paste the program code into part 2(c) in the evidence document.
[6]
(d) The function Dequeue():
•
returns a null or empty record if the circular queue is empty
•
returns the first record in the queue if the circular queue is not empty
•
updates pointers and other variables as required.
Write program code for the function Dequeue().
Save your program.
Copy and paste the program code into part 2(d) in the evidence document.
[6]
(e) The procedure EnterRecord():
•
takes an ID and quantity as input and creates a sale record
•
uses Enqueue() to insert the record in the circular queue
•
outputs "Full" if the record was not inserted in the circular queue
•
outputs "Stored" if the record was inserted in the circular queue.
Write program code for the procedure EnterRecord().
Save your program.
Copy and paste the program code into part 2(e) in the evidence document.
[5]
(f)
The following sale records need to be entered into the program:
ID
Quantity
ADF
10
OOP
1
BXW
5
XXZ
22
HQR
6
LLP
3
(i)
Amend the main program to:
•
use EnterRecord() to input the six records in the table
•
use Dequeue() to remove one record
•
output either the ID and quantity of the removed record, or an error message if the
circular queue is empty
•
use EnterRecord() to input the record with the ID "LLP" for a second time
•
output the ID and quantity for all the records currently stored in the array
CircularQueue.
Write program code to perform these tasks.
Save your program.
Copy and paste the program code into part 2(f)(i) in the evidence document.
[4]
(ii)
Test your program.
Take a screenshot of the output.
Save your program.
Copy and paste the screenshot into part 2(f)(ii) in the evidence document.
[1]
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]
Question text
3
A binary tree consists of nodes. Each node has 3 integer values: a left pointer, data and a right
pointer.
The binary tree is stored using a global 2D array.
The pseudocode declaration for the array is:
DECLARE ArrayNodes : ARRAY[0:19, 0:2] OF INTEGER
For example:
•
ArrayNodes[0, 0] stores the left pointer for the first node.
•
ArrayNodes[0, 1] stores the data for the first node.
•
ArrayNodes[0, 2] stores the right pointer for the first node.
–1 indicates a null pointer, or null data.
(a) Write program code to:
•
declare the global 2D array ArrayNodes
•
initialise all 3 integer values to –1 for each node.
Save your program as Question3_N22.
Copy and paste the program code into part 3(a) in the evidence document.
[3]
(b) The binary tree stores the following values:
Index
Left pointer
Data
Right pointer
0
1
20
5
1
2
15
–1
2
–1
3
3
3
–1
9
4
4
–1
10
–1
5
–1
58
–1
6
–1
–1
–1
FreeNode stores the index of the first free element in the array, initialised to 6.
RootPointer stores the index of the first node in the tree, initialised to 0.
Amend your program by writing program code to store the given data in ArrayNodes and
initialise the free node and root node pointers.
Save your program.
Copy and paste the program code into part 3(b) in the evidence document.
[2]
(c) The following recursive pseudocode function searches the binary tree for a given value. If the
value is found, the function must return the index of the value. If the value is not found, the
function must return –1.
The function is incomplete. There are four incomplete statements.
FUNCTION SearchValue(Root : INTEGER,
ValueToFind : INTEGER) RETURNS INTEGER
IF Root = –1 THEN
RETURN –1
ELSE
IF ArrayNodes[Root, 1] = ValueToFind THEN
RETURN …
ELSE
IF ArrayNodes[Root, 1] = –1 THEN
RETURN –1
ENDIF
ENDIF
ENDIF
IF ArrayNodes[Root, 1] … ValueToFind THEN
RETURN SearchValue(ArrayNodes[ … , 0], ValueToFind)
ENDIF
IF ArrayNodes[Root, … ] < ValueToFind THEN
RETURN SearchValue(ArrayNodes[Root, 2], ValueToFind)
ENDIF
ENDFUNCTION
Write program code for the function SearchValue().
Save your program.
Copy and paste the program code into part 3(c) in the evidence document.
[5]
(d) A post order traversal performs the following operation:
•
visit the left node
•
visit the right node
•
output the root.
For example, in the following tree, the output would be: 3
9
25
60
50
50
25
60
3
9
An outline of the PostOrder() procedure is:
•
If left node is not empty, make a recursive call with the left node as the root.
•
If right node is not empty, make a recursive call with the right node as the root.
•
Output the current root node.
The procedure PostOrder() takes the root node as a parameter.
Write program code for the procedure PostOrder().
Save your program.
Copy and paste the program code into part 3(d) in the evidence document.
[7]
(e) (i)
Amend the main program by writing program code to:
•
call the function SearchValue() to find the position of the number 15 in the tree
•
use the result from SearchValue() to output either the index of the value if found,
or an appropriate message to state that the value was not found
•
call the procedure PostOrder().
Save your program.
Copy and paste the program code into part 3(e)(i) in the evidence document.
[3]
(ii)
Test your program.
Take a screenshot to show the output.
Copy and paste the screenshot 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.
Question text
1
A computer program is needed to store jobs in order of priority. Each job has a job number
(for example, 123) and a priority from 1 to 10, with 1 being the highest priority and 10 the lowest.
The program stores the jobs in a global 2D array.
The pseudocode declaration for the array is:
DECLARE Jobs : ARRAY[0:99, 0:1] OF INTEGER
For example:
•
Jobs[0, 0] stores the job number of the first job.
•
Jobs[0, 1] stores the priority of the first job.
The global variable, NumberOfJobs, stores the number of jobs currently in the array.
(a) Write program code to declare the global 2D array Jobs and the global variable
NumberOfJobs.
Save your program as Question1_N22.
Copy and paste the program code into part 1(a) in the evidence document.
[3]
(b) The procedure Initialise() stores –1 in each of the array elements and assigns 0 to
NumberOfJobs.
Write program code for the procedure Initialise().
Save your program.
Copy and paste the program code into part 1(b) in the evidence document.
[3]
(c) The procedure AddJob():
•
takes a job number and priority as parameters
•
stores the job in the next free array element
•
outputs ‘Added’ if the job was successfully stored in the array
•
outputs ‘Not added’ if the job was not successfully stored in the array.
Write program code for the procedure AddJob().
Save your program.
Copy and paste the program code into part 1(c) in the evidence document.
[5]
(d) The main program should call the procedure Initialise() and then use the AddJob()
procedure to store the following jobs in the order given:
Job number
Priority
12
10
526
9
33
8
12
9
78
1
Write program code for the main program and perform the tasks described.
Save your program.
Copy and paste the program code into part 1(d) in the evidence document.
[2]
(e) When a new job has been added, the array is sorted into ascending numerical order of priority
using an insertion sort.
Write program code for the procedure InsertionSort() to sort the data into ascending
numerical order of priority.
Save your program.
Copy and paste the program code into part 1(e) in the evidence document.
[5]
(f)
The procedure PrintArray() outputs each job number and priority on a line, for example:
123 priority 1
39 priority 3
120 priority 7
Write program code for the procedure PrintArray().
Save your program.
Copy and paste the program code into part 1(f) in the evidence document.
[3]
(g) The main program needs to sort the array and then output the contents of the array.
(i)
Amend the main program by writing program code to call procedures InsertionSort()
and PrintArray().
Save your program.
Copy and paste the program code into part 1(g)(i) in the evidence document.
[1]
(ii)
Test your program.
Take a screenshot of the output.
Copy and paste the screenshot into part 1(g)(ii) in the evidence document.
[1]
Question text
2
A computer game is being developed. The game has 10 different characters that are all active in
the game.
Part of the game is being written using object-oriented programming.
The class Character stores data about the characters. Each character has a name and the x
coordinate and y coordinate of their current position.
Character
Name : STRING
stores the name of the character
XCoordinate : INTEGER
stores the x coordinate
YCoordinate : INTEGER
stores the y coordinate
Constructor()
initialises Name, XCoordinate and YCoordinate from the
values passed as parameters
GetName()
returns the name of the character
GetX()
returns the x coordinate of the character
GetY()
returns the y coordinate of the character
ChangePosition()
takes XChange as an integer parameter and adds it to the
x coordinate
takes YChange as an integer parameter and adds it to the
y coordinate
(a) Write program code to declare the class Character and its constructor. Do not write
program code for the other methods.
Use your programming language appropriate constructor.
All attributes must be private. If you are writing in Python, include attribute declarations using
comments.
Save your program as Question2_N22.
Copy and paste the program code into part 2(a) in the evidence document.
[4]
(b) Write program code for the three get methods for the class Character.
Save your program.
Copy and paste the program code into part 2(b) in the evidence document.
[3]
(c) Write program code for the method ChangePosition().
Save your program.
Copy and paste the program code into part 2(c) in the evidence document.
[2]
(d) The main program has a 1D array of characters. Each character is stored as an object of type
Character.
The game has a maximum of 10 characters. The character names, x coordinates and y
coordinates are stored in the file Characters.txt in the order:
•
name
•
x coordinate
•
y coordinate.
For example, the first character in the file is named Amal, with the x coordinate 0 and the y
coordinate 2.
Amend the main program by writing program code to:
•
declare the array
•
read in all 10 characters from Characters.txt
•
store each character as an object in the array.
Save your program.
Copy and paste the program code into part 2(d) in the evidence document.
[7]
(e) The main program needs to read in a character’s name from the user, search for the character
in the array and store the index of its position. It repeats until the user enters a name that
exists in the array.
Amend the main program by writing program code to perform this task.
Save your program.
Copy and paste the program code into part 2(e) in the evidence document.
[5]
(f)
The user will enter a letter to identify the direction the chosen character from part 2(e) should
move.
•
If ‘A’ is input, the character moves left (x coordinate minus 1).
•
If ‘W’ is input, the character moves up (y coordinate plus 1).
•
If ‘S’ is input, the character moves down (y coordinate minus 1).
•
If ‘D’ is input, the character moves right (x coordinate plus 1).
Amend the main program by writing program code to:
•
take a letter as input until it is a valid move (A, W, S or D)
•
change the position of the character using the appropriate method.
Save your program.
Copy and paste the program code into part 2(f) in the evidence document.
[7]
(g) (i)
When a change to a character’s position has been made, the program needs to output
the character’s name and the new x and y coordinates of the character, in the format:
Qui has changed coordinates to X = 83 and Y = 0
Amend the main program by writing program code to perform these tasks.
Save your program.
Copy and paste the program code into part 2(g)(i) in the evidence document.
[2]
(ii)
Test your program by inputting the following four items of data in the order given:
THOMAS
qui
X
A
Take a screenshot of the output.
Copy and paste the screenshot into part 2(g)(ii) in the evidence document.
[1]
Question text
3
A program uses a linear queue to store up to 100 integers.
(a) A 1D array, Queue, is used to store the data. The head pointer points to the first number
stored in the queue and the tail pointer points to the next free space in the queue.
Write program code to:
•
declare the global array Queue
•
declare the global variable head pointer and assign an appropriate initial value
•
declare the global variable tail pointer and assign an appropriate initial value.
Save your program as Question3_N22.
Copy and paste the program code into part 3(a) in the evidence document.
[3]
(b) The function Enqueue() takes an integer value as a parameter and stores it in the queue. It
returns TRUE if the value was successfully stored and FALSE otherwise.
Write program code for the function Enqueue().
Save your program.
Copy and paste the program code into part 3(b) in the evidence document.
[6]
(c) The main program uses the Enqueue() function to store the numbers 1 to 20 (inclusive)
in the queue, in ascending numerical order. The program should output ‘Successful’ if all
numbers are successfully enqueued, and ‘Unsuccessful’ otherwise.
Amend the main program by writing program code to perform this task.
Save your program.
Copy and paste the program code into part 3(c) in the evidence document.
[4]
(d) The following iterative pseudocode function calculates the total of all the values stored in the
queue.
FUNCTION IterativeOutput(Start: INTEGER) RETURNS INTEGER
DECLARE Total : INTEGER
← 0
Total
FOR Count ← Start - 1 TO HeadPointer STEP -1
Total ← Total + Queue[Count]
NEXT Count
RETURN Total
ENDFUNCTION
Rewrite the function as a recursive function using program code.
Save your program.
Copy and paste the program code into part 3(d) in the evidence document.
[6]
(e) The main program calls the recursive function from part 3(d) and outputs the value returned.
(i)
Amend the main program by writing program code to perform this task.
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 to show the output.
Copy and paste the screenshot 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
1
The text file HighScore.txt stores the players who have scored the top ten scores in a game,
in descending order of score. The file stores the 3-character name of the player, and their integer
score, in the order: player, score.
For example, the current top player in the text file:
FYI is the player name
10 000 is the score
The program:
•
reads in the data from HighScore.txt
•
allows the user to enter a new player name and their score
•
if appropriate, inserts the new player (name and score) into the top ten
•
writes the top ten players (name and score) into a new text file NewHighScore.txt
(a) The program stores the players and their scores in an array of 11 elements (10 elements to
be read from the file, 1 element to be inserted by the user).
Write a program to declare one or more arrays, as global data structures, to store the player
names and their scores.
Save your program as Question1_J2022.
Copy and paste the program code into part 1(a) in the evidence document.
[2]
(b) The procedure ReadHighScores() opens the file HighScore.txt and reads the data into
the data structure(s) declared in part 1(a).
Write program code to declare the procedure ReadHighScores().
Save your program.
Copy and paste the program code into part 1(b) in the evidence document.
[6]
(c) The procedure OutputHighScores() outputs all the values in the data structure(s) in the
format:
PlayerName Score
For example, the first two data items:
FYI 10 000
ABC 9 092
Write program code to declare the procedure OutputHighScores().
Save your program.
Copy and paste the program code into part 1(c) in the evidence document.
[3]
(d) The main program should first call ReadHighScores() and then OutputHighScores().
(i)
Write the program code for the main program.
Save your program.
Copy and paste the program code into part 1(d)(i) in the evidence document.
[2]
(ii)
Test your program.
Take a screenshot to show the output from part 1(d)(i).
Copy and paste the screenshot into part 1(d)(ii) in the evidence document.
[1]
(e) The main program needs to ask the user to input a new player name and a score. If this score
is in the top ten then it will create a new top ten list that includes this score.
(i)
Amend the main program to ask the user to input a 3-character player name and an
integer score that must be between 1 and 100 000 inclusive.
Save your program.
Copy and paste the program code into part 1(e)(i) in the evidence document.
[3]
(ii)
Write program code to declare a procedure that:
•
takes the player name and score as parameters
•
creates a new top ten list that includes the parameter if appropriate.
Save your program.
Copy and paste the program code into part 1(e)(ii) in the evidence document.
[5]
(iii)
Amend the main program to call the procedure from part 1(e)(ii).
Output the contents of the array before inserting the new player name and score, and
output the contents of the array after inserting the new player name and score.
Save your program.
Copy and paste the program code into part 1(e)(iii) in the evidence document.
[2]
(iv)
Test your program by entering the player name "JKL" and the score "9999".
Take a screenshot to show the output.
Copy and paste the screenshot into part 1(e)(iv) in the evidence document.
[1]
(f)
The procedure WriteTopTen() stores the new top ten player names and scores in a text
file called NewHighScore.txt
Write program code to declare the procedure WriteTopTen().
Save your program.
Copy and paste the program code into part 1(f) in the evidence document.
[4]
Question text
3
A program uses a circular queue to store strings. The queue is created as a 1D array, QueueArray,
with 10 string items.
The following data is stored about the queue:
•
the head pointer initialised to 0
•
the tail pointer initialised to 0
•
the number of items in the queue initialised to 0.
(a) Declare the array, head pointer, tail pointer and number of items.
If you are writing in Python, include attribute declarations using comments.
Save your program as Question3_J2022.
Copy and paste the program code into part 3(a) in the evidence document.
[2]
(b) The function Enqueue is written in pseudocode. The function adds DataToAdd to the queue.
It returns FALSE if the queue is full and returns TRUE if the item is added.
The function is incomplete, there are five incomplete statements.
FUNCTION Enqueue(BYREF QueueArray[] : STRING, BYREF HeadPointer : INTEGER,
BYREF TailPointer : INTEGER, NumberItems : INTEGER,
DataToAdd : STRING) RETURNS BOOLEAN
IF NumberItems = … THEN
RETURN …
ENDIF
← DataToAdd
QueueArray[ … ]
IF TailPointer >= 9 THEN
← …
TailPointer
ELSE
← TailPointer + 1
TailPointer
ENDIF
← NumberItems …
NumberItems
RETURN TRUE
ENDFUNCTION
Write program code for the function Enqueue().
Save your program.
Copy and paste the program code into part 3(b) in the evidence document.
[7]
(c) The function Dequeue() returns "FALSE" if the queue is empty, or it returns the next data
item in the queue.
Write program code for the function Dequeue().
Save your program.
Copy and paste the program code into part 3(c) in the evidence document.
[6]
(d) (i)
Amend the main program to:
•
take as input 11 string values from the user
•
use the Enqueue() function to add each element to the queue
•
output an appropriate message to state whether each addition was successful, or
not
•
call Dequeue() function twice and output the return value each time.
Save your program.
Copy and paste the program code into part 3(d)(i) in the evidence document.
[5]
(ii)
Test your program with the input data:
"A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K"
Take a screenshot to show the output.
Copy and paste the screenshot into part 3(d)(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
1
A program needs to use a stack data structure. The stack can store up to 10 integer elements.
A 1D array StackData is used to store the stack globally. The global variable StackPointer
points to the next available space in the stack and is initialised to 0.
(a) Write program code to declare the array and pointer as global data structures. Initialise the
pointer to 0.
Save your program as Question1_J22.
Copy and paste the program code into part 1(a) in the evidence document.
[3]
(b) Write a procedure to output all 10 elements in the stack and the value of StackPointer.
Save your program.
Copy and paste the program code into part 1(b) in the evidence document.
[3]
(c) The function Push() takes an integer parameter and returns FALSE if the stack is full. If the
stack is not full, it puts the parameter value on the stack, updates the relevant pointer and
returns TRUE.
Write program code for the function Push().
Save your program.
Copy and paste the program code into part 1(c) in the evidence document.
[6]
(d) (i)
Edit the main program to test the Push() function. The main program needs to:
•
allow the user to enter 11 numbers and attempt to add these to the stack
•
output an appropriate message when a number is added to the stack
•
output an appropriate message when a number is not added to the stack if it is full
•
output the contents of the stack after attempting to add all 11 numbers.
Save your program.
Copy and paste the program code into part 1(d)(i) in the evidence document.
[5]
(ii)
Test your program from part 1(d)(i) with the following 11 inputs:
11
12
13
14
15
16
17
18
19
20
21
Take a screenshot to show the output.
Copy and paste the screenshot into part 1(d)(ii) in the evidence document.
[1]
(e) The function Pop() returns −1 if the stack is empty. If the stack is not empty, it returns the
element at the top of the stack and updates the relevant pointer.
(i)
Write program code for the function Pop().
Save your program.
Copy and paste the program code into part 1(e)(i) in the evidence document.
[5]
(ii)
After the code you wrote in the main program for part 1(d)(i), add program code to:
•
remove two elements from the stack using Pop()
•
output the updated contents of the stack.
Test your program and take a screenshot to show the output.
Copy and paste the screenshot into part 1(e)(ii) in the evidence document.
[2]
Question text
2
A 2D array stores data entered by a user.
(a) The main program declares a 2D array of 10 by 10 integer elements.
The array is initialised with a random number between 1 and 100 in each element.
Write program code for the main program.
Save your program as Question2_J22.
Copy and paste the program code into part 2(a) in the evidence document.
[4]
(b) The following bubble sort pseudocode algorithm sorts the data in the first dimension of the 2D
array into ascending numerical order.
10
ArrayLength
0 TO ArrayLength - 1
FOR X
0 TO ArrayLength - 2
FOR Y
0 TO ArrayLength - Y - 2
FOR Z
IF ArrayData[X, Z] > ArrayData[X, Z + 1] THEN
ArrayData[X, Z]
TempValue
ArrayData[X, Z+1]
ArrayData[X, Z]
TempValue
ArrayData[X, Z + 1]
ENDIF
NEXT Z
NEXT Y
NEXT X
(i)
Amend your main program by writing program code to implement the bubble sort
algorithm after the initialisation of the array elements.
You must not use any built-in sorting functions for your programming language.
Save your program.
Copy and paste the program code into part 2(b)(i) in the evidence document.
[5]
(ii)
Write program code for a procedure to output all the values in the 2D array. The values
should be output as a 2D grid, with values in rows and columns.
Call the procedure before and after your bubble sort code.
Save your program.
Copy and paste the program code into part 2(b)(ii) in the evidence document.
[3]
(iii)
Test your program.
Take a screenshot to show the output.
Copy and paste the screenshot into part 2(b)(iii) in the evidence document.
[1]
(c) The following pseudocode function uses recursion to perform a binary search in the first row
of the array, for the value SearchValue in the array SearchArray.
The function returns −1 if the item was not found, or it returns the index where it is found.
There are six incomplete statements.
FUNCTION BinarySearch(SearchArray, Lower, Upper, SearchValue)RETURNS
INTEGER
IF Upper >= Lower THEN
(Lower + (Upper – 1)) DIV …
Mid
IF SearchArray[0, Mid] = … THEN
RETURN …
ELSE
IF SearchArray[0, Mid] > SearchValue THEN
RETURN BinarySearch(SearchArray, … , Mid – 1,
SearchValue)
ELSE
RETURN BinarySearch(SearchArray, Mid + 1, … ,
SearchValue)
ENDIF
ENDIF
ENDIF
RETURN …
ENDFUNCTION
Note: the arithmetic operator DIV performs integer division, e.g. the result of 10 DIV 3 will be 3.
(i)
Write program code for the recursive function BinarySearch().
Save your program.
Copy and paste the program code into part 2(c)(i) in the evidence document.
[8]
(ii)
In the main program, test the function BinarySearch() twice, outputting the returned
value each time.
One test should be for a number that is in the first line of the array.
One test should be for a number that is not in the first line of the array.
Take a screenshot to show the output.
Copy and paste the screenshot into part 2(c)(ii) in the evidence document.
[2]
Question text
3
A programmer is designing a computer game that uses a set of cards.
Each card has a number and a colour. The cards are saved in the text file CardValues.txt
The program has a class named Card. The class has the following attributes and methods.
Card
Number : INTEGER
The number of the card
Colour : STRING
The colour of the card
Constructor()
Takes two values as parameters and sets them to the private
attributes
GetNumber()
Returns the number of the card
Returns the colour of the card
GetColour()
(a) The constructor takes the number and colour of the card as parameters and sets them to the
private attributes.
Write program code to declare the class Card and its constructor. Do not write any other
methods.
Use your programming language appropriate constructor.
All attributes should be private. If you are writing in Python, include attribute declarations
using comments.
Save your program as Question3_J22.
Copy and paste the program code into part 3(a) in the evidence document.
[5]
(b) The two get methods return the associated attribute.
Write program code for the get methods GetNumber() and GetColour().
Save your program.
Copy and paste the program code into part 3(b) in the evidence document.
[3]
(c) The text file CardValues.txt stores the data for 30 cards, in the order: number, colour.
For example, the first card in the text file:
1 is the number
red is the colour.
A 1D array of type Card is declared to store all the cards read in from CardValues.txt
Write the main program to:
•
declare an array of type Card with 30 elements
•
read in the data for the 30 cards from CardValues.txt and assign each to the array.
Save your program.
Copy and paste the program code into part 3(c) in the evidence document.
[7]
(d) The program needs to allow all players (maximum of 5) to select 4 cards from the 30 available.
A card can only be selected once, so the program needs to record which cards have already
been selected.
The function, ChooseCard():
•
takes as input an integer to represent an array index from 1 to 30
•
validates that the value is between 1 and 30 inclusive
•
checks if the card is available (it has not already been selected)
•
loops until an available card is selected
•
returns the index of the card if it is available.
Amend the program to store which cards have already been selected and write program
code for the function ChooseCard().
Save your program.
Copy and paste the program code into part 3(d) in the evidence document.
[6]
(e) The main program needs to allow one player to select all their 4 cards.
(i)
Amend the main program to:
•
create an array, Player1, for player 1 of type Card
•
ask player 1 to input 4 integers using the function from part 3(d)
•
store the cards in Player1
•
output the number and colour of the 4 cards in Player1.
Save your program.
Copy and paste the program code into part 3(e)(i) in the evidence document.
[5]
(ii)
Test your program with the following test data:
Test 1: 1
5
9
10
Test 2: 2
2
3
4
4
5
Take a screenshot to show the output.
Copy and paste the screenshot 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