Computational thinking: searching, sorting, ADTs and recursion – A Level Computer Science (9618)
7 questions · Clear filters
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
1
This iterative pseudocode algorithm for the function IterativeVowels() takes a string as a
parameter and counts the number of lower-case vowels in this string.
The vowels are the letters a, e, i, o and u.
FUNCTION IterativeVowels(Value : STRING) RETURNS INTEGER
DECLARE Total : INTEGER
DECLARE LengthString : INTEGER
DECLARE FirstCharacter : CHAR
← 0
Total
← LENGTH(Value)
LengthString
← 0 TO LengthString - 1
FOR X
FirstCharacter ← MID(Value, 0, 1)
IF FirstCharacter = 'a' OR FirstCharacter = 'e' OR
FirstCharacter = 'i' OR FirstCharacter = 'o' OR
FirstCharacter = 'u' THEN
← Total + 1
Total
ENDIF
← MID(Value, 1, LENGTH(Value)-1)
Value
NEXT X
RETURN Total
ENDFUNCTION
The pseudocode function MID(X, Y, Z) returns Z number of characters from string X, starting
at the character in position Y. The first character in a string is in position 0, for example:
MID("computer", 0, 3) returns "com"
The pseudocode function LENGTH(X) returns the number of characters in the string X, for
example:
LENGTH("computer") returns 8
(a) (i)
Write program code for the function IterativeVowels().
Save your program as Question1_N23.
Copy and paste the program code into part 1(a)(i) in the evidence document.
[5]
(ii)
Write program code to call the function IterativeVowels() with the parameter
"house" from the main program.
Output the return value.
Save your program.
Copy and paste the program code into part 1(a)(ii) in the evidence document.
[2]
(iii)
Test your program.
Take a screenshot of the output.
Save your program.
Copy and paste the screenshot into part 1(a)(iii) in the evidence document.
[1]
(b) (i)
Rewrite the function IterativeVowels() as a recursive function with the identifier
RecursiveVowels().
Save your program.
Copy and paste the program code into part 1(b)(i) in the evidence document.
[6]
(ii)
Write program code to call the function RecursiveVowels() with the parameter
"imagine" from the main program.
Output the return value.
Save your program.
Copy and paste the program code into part 1(b)(ii) in the evidence document.
[1]
(iii)
Test your program.
Take a screenshot of the output.
Save your program.
Copy and paste the screenshot into part 1(b)(iii) in the evidence document.
[1]
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
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
3
An ordered binary tree stores integer data in ascending numerical order.
The data for the binary tree is stored in a 2D array with the following structure:
LeftPointer
Data
RightPointer
Index
[0]
[1]
[2]
[0]
1
10
2
[1]
-1
5
-1
[2]
-1
16
-1
Each row in the table represents one node on the tree.
The number -1 represents a null pointer.
(a) The 2D array, ArrayNodes, is declared with space for 20 nodes.
Each node has a left pointer, data and right pointer.
The program also initialises the:
•
RootPointer to -1 (null); this points to the first node in the binary tree
•
FreeNode to 0; this points to the first empty node in the array.
Write program code to declare ArrayNodes, RootPointer and FreeNode in the main
program.
If you are writing in Python programming language, include attribute declarations using
comments.
Save your program as question 3.
Copy and paste the program code into part 3(a) in the evidence document.
[4]
(b) The procedure AddNode() adds a new node to the array ArrayNodes.
The procedure needs to:
•
take the array, root pointer and free node pointer as parameters
•
ask the user to enter the data and read this in
•
add the node to the root pointer if the tree is empty
•
otherwise, follow the pointers to find the position for the data item to be added
•
store the data in the location and update all pointers.
There are six incomplete statements in the following pseudocode for the procedure AddNode().
PROCEDURE AddNode(BYREF ArrayNodes[] : ARRAY OF INTEGER,
BYREF RootPointer : INTEGER, BYREF FreeNode : INTEGER)
OUTPUT "Enter the data"
INPUT NodeData
IF FreeNode <= 19 THEN
←
-1
ArrayNodes[FreeNode, 0]
←
…
ArrayNodes[FreeNode, 1]
←
-1
ArrayNodes[FreeNode, 2]
IF RootPointer = … THEN
←
0
RootPointer
ELSE
←
FALSE
Placed
←
RootPointer
CurrentNode
WHILE Placed = FALSE
IF NodeData < ArrayNodes[CurrentNode, 1] THEN
IF ArrayNodes[CurrentNode, 0] = -1 THEN
←
…
ArrayNodes[CurrentNode, 0]
←
TRUE
Placed
ELSE
←
ArrayNodes[CurrentNode, 0]
…
ENDIF
ELSE
IF ArrayNodes[CurrentNode, 2] = -1 THEN
←
FreeNode
ArrayNodes[CurrentNode, 2]
←
…
Placed
ELSE
←
ArrayNodes[CurrentNode, 2]
CurrentNode
ENDIF
ENDIF
ENDWHILE
ENDIF
←
… + 1
FreeNode
ELSE
OUTPUT("Tree is full")
ENDIF
ENDPROCEDURE
Write program code for the procedure AddNode().
Save your program.
Copy and paste the program code into part 3(b) in the evidence document.
[8]
(c) The procedure PrintAll() outputs the data in each element in ArrayNodes, in the order
they are stored in the array.
Each element is printed in a row in the order:
LeftPointer
Data
RightPointer
For example:
1
20
-1
-1
10
-1
Write program code for the procedure PrintAll().
Save your program.
Copy and paste the program code into part 3(c) in the evidence document.
[4]
(d) The main program should loop 10 times, each time calling the procedure AddNode(). It
should then call the procedure PrintAll().
(i)
Edit the main program to perform the actions described.
Save your program.
Copy and paste the program code into part 3(d)(i) in the evidence document.
[3]
(ii)
Test the program by entering the data:
10
5
15
8
12
6
20
11
9
4
Take a screenshot to show the output after the given data are entered.
Copy and paste the screenshot into part 3(d)(ii) in the evidence document.
[1]
(e) An in-order tree traversal visits the left node, then the root (and outputs this), then visits the
right node.
(i)
Write a recursive procedure, InOrder(), to perform an in-order traversal on the tree
held in ArrayNodes.
Save your program.
Copy and paste the program code into part 3(e)(i) in the evidence document.
[7]
(ii)
Test the procedure InOrder() with the same data entered in part (d)(ii).
Take a screenshot to show the output after entering the data.
Copy and paste the screenshot into part 3(e)(ii) in the evidence document.
[1]
Permission to reproduce items where third-party owned materia
Question text
3
An ordered binary tree stores integer data in ascending numerical order.
The data for the binary tree is stored in a 2D array with the following structure:
LeftPointer
Data
RightPointer
Index
[0]
[1]
[2]
[0]
1
10
2
[1]
-1
5
-1
[2]
-1
16
-1
Each row in the table represents one node on the tree.
The number -1 represents a null pointer.
(a) The 2D array, ArrayNodes, is declared with space for 20 nodes.
Each node has a left pointer, data and right pointer.
The program also initialises the:
•
RootPointer to -1 (null); this points to the first node in the binary tree
•
FreeNode to 0; this points to the first empty node in the array.
Write program code to declare ArrayNodes, RootPointer and FreeNode in the main
program.
If you are writing in Python programming language, include attribute declarations using
comments.
Save your program as question 3.
Copy and paste the program code into part 3(a) in the evidence document.
[4]
(b) The procedure AddNode() adds a new node to the array ArrayNodes.
The procedure needs to:
•
take the array, root pointer and free node pointer as parameters
•
ask the user to enter the data and read this in
•
add the node to the root pointer if the tree is empty
•
otherwise, follow the pointers to find the position for the data item to be added
•
store the data in the location and update all pointers.
There are six incomplete statements in the following pseudocode for the procedure AddNode().
PROCEDURE AddNode(BYREF ArrayNodes[] : ARRAY OF INTEGER,
BYREF RootPointer : INTEGER, BYREF FreeNode : INTEGER)
OUTPUT "Enter the data"
INPUT NodeData
IF FreeNode <= 19 THEN
←
-1
ArrayNodes[FreeNode, 0]
←
…
ArrayNodes[FreeNode, 1]
←
-1
ArrayNodes[FreeNode, 2]
IF RootPointer = … THEN
←
0
RootPointer
ELSE
←
FALSE
Placed
←
RootPointer
CurrentNode
WHILE Placed = FALSE
IF NodeData < ArrayNodes[CurrentNode, 1] THEN
IF ArrayNodes[CurrentNode, 0] = -1 THEN
←
…
ArrayNodes[CurrentNode, 0]
←
TRUE
Placed
ELSE
←
ArrayNodes[CurrentNode, 0]
…
ENDIF
ELSE
IF ArrayNodes[CurrentNode, 2] = -1 THEN
←
FreeNode
ArrayNodes[CurrentNode, 2]
←
…
Placed
ELSE
←
ArrayNodes[CurrentNode, 2]
CurrentNode
ENDIF
ENDIF
ENDWHILE
ENDIF
←
… + 1
FreeNode
ELSE
OUTPUT "Tree is full"
ENDIF
ENDPROCEDURE
Write program code for the procedure AddNode().
Save your program.
Copy and paste the program code into part 3(b) in the evidence document.
[8]
(c) The procedure PrintAll() outputs the data in each element in ArrayNodes, in the order
they are stored in the array.
Each element is printed in a row in the order:
LeftPointer
Data
RightPointer
For example:
1
20
-1
-1
10
-1
Write program code for the procedure PrintAll().
Save your program.
Copy and paste the program code into part 3(c) in the evidence document.
[4]
(d) The main program should loop 10 times, each time calling the procedure AddNode(). It
should then call the procedure PrintAll().
(i)
Edit the main program to perform the actions described.
Save your program.
Copy and paste the program code into part 3(d)(i) in the evidence document.
[3]
(ii)
Test the program by entering the data:
10
5
15
8
12
6
20
11
9
4
Take a screenshot to show the output after the given data are entered.
Copy and paste the screenshot into part 3(d)(ii) in the evidence document.
[1]
(e) An in-order tree traversal visits the left node, then the root (and outputs this), then visits the
right node.
(i)
Write a recursive procedure, InOrder(), to perform an in-order traversal on the tree
held in ArrayNodes.
Save your program.
Copy and paste the program code into part 3(e)(i) in the evidence document.
[7]
(ii)
Test the procedure InOrder() with the same data entered in part (d)(ii).
Take a screenshot to show the output after entering the data.
Copy and paste the screenshot into part 3(e)(ii) in the evidence document.
[1]
Permission to reproduce items where third-party owned material