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

50 questions · page 4 of 4 · Clear filters

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

1
Study the following pseudocode for a recursive function.
FUNCTION Unknown(BYVAL X, BYVAL Y : INTEGER) RETURNS INTEGER
IF X < Y THEN
OUTPUT X + Y
RETURN (Unknown(X + 1, Y) * 2)
ELSE
IF X = Y THEN
RETURN 1
ELSE
OUTPUT X + Y
RETURN (Unknown(X - 1, Y) DIV 2)
ENDIF
ENDIF
ENDFUNCTION
The operator DIV returns the integer value after division e.g. 13 DIV 2 would give 6
(a) Write program code to declare the function Unknown().
Save your program as question 1.
Copy and paste the program code into part 1(a) in the evidence document.
[3]
(b) The main program needs to run all three of the following function calls and output the result
of each call:
Unknown(10, 15)
Unknown(10, 10)
Unknown(15, 10)
(i)
For each of the three function calls, the main program needs to:
•
output the value of the two parameters
•
call the function with those parameters
•
output the return value.
Write the program code for the main program.
Save your program.
Copy and paste the program code into part 1(b)(i) in the evidence document.
[3]
(ii)
Take a screenshot to show the output from part (b)(i).
Copy and paste the screenshot into part 1(b)(ii) in the evidence document.
[2]
(c) Rewrite the function Unknown() as an iterative function, IterativeUnknown().
Save your program.
Copy and paste the program code into part 1(c) in the evidence document.
[7]
(d) The iterative function needs to be called three times with the same parameters as in part (b).
(i)
For each of the three function calls, the main program needs to:
•
output the value of the two parameters
•
call the iterative function with those parameters
•
output the return value.
Amend the main program to perform these tasks.
Save your program.
Copy and paste the program code into part 1(d)(i) in the evidence document.
[1]
(ii)
Take one or more screenshots to show the output of both functions for each set of
parameters.
Copy and paste the screenshot(s) into part 1(d)(ii) in the evidence document.
[1]

47
9618/41/O/N/21 · Question 3Oct/Nov 2021Paper 4134 marks
Loading question…
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

48
9618/42/O/N/21 · Question 3Oct/Nov 2021Paper 4234 marks
Loading question…
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

Advertisement
49
9618/41/M/J/21 · Question 1May/June 2021Paper 4124 marks
Loading question…
Question text

1
An unordered linked list uses a 1D array to store the data.
Each item in the linked list is of a record type, node, with a field data and a field nextNode.
The current contents of the linked list are:
startPointer
data
nextNode
0
Index
0
1
1
emptyList
5
1
5
4
2
6
7
3
7
-1
4
2
2
5
0
6
6
0
8
7
56
3
8
0
9
9
0
-1
(a) The following is pseudocode for the record type node.
TYPE node
DECLARE data : INTEGER
DECLARE nextNode : INTEGER
ENDTYPE
Write program code to declare the record type node.
Save your program as question1.
Copy and paste the program code into part 1(a) in the evidence document.
[2]
(b) Write program code for the main program.
Declare a 1D array of type node with the identifier linkedList, and initialise it with the data
shown in the table on page 2. Declare the pointers.
Save your program.
Copy and paste the program code into part 1(b) in the evidence document.
[4]
(c) The procedure outputNodes() takes the array and startPointer as parameters. The
procedure outputs the data from the linked list by following the nextNode values.
(i)
Write program code for the procedure outputNodes().
Save your program.
Copy and paste the program code into part 1(c)(i) in the evidence document.
[6]
(ii)
Edit the main program to call the procedure outputNodes().
Take a screenshot to show the output of the procedure outputNodes().
Save your program.
Copy and paste the screenshot into part 1(c)(ii) in the evidence document.
[1]
(d) The function, addNode(), takes the linked list and pointers as parameters, then takes as
input the data to be added to the end of the linkedList.
The function adds the node in the next available space, updates the pointers and returns
True. If there are no empty nodes, it returns False.
(i)
Write program code for the function addNode().
Save your program.
Copy and paste the program code into part 1(d)(i) in the evidence document.
[7]
(ii)
Edit the main program to:
•
call addNode()
•
output an appropriate message depending on the result returned from addNode()
•
call outputNodes() twice; once before calling addNode() and once after calling
addNode().
Save your program.
Copy and paste the program code into part 1(d)(ii) in the evidence document.
[3]
(iii)
Test your program by inputting the data value 5 and take a screenshot to show the
output.
Save your program.
Copy and paste the screenshot into part 1(d)(iii) in the evidence document.
[1]

50
9618/41/M/J/21 · Question 2May/June 2021Paper 4120 marks
Loading question…
Question text

2
A program stores the following ten integers in a 1D array with the identifier arrayData.
10
5
6
7
1
12
13
15
21
8
(a) Write program code for a new program to:
•
declare the global 1D array, arrayData, with ten elements
•
initialise arrayData in the main program using the data values shown.
Save your program as question2.
Copy and paste the program code into part 2(a) in the evidence document.
[2]
(b) (i)
A function, linearSearch(), takes an integer as a parameter and performs a linear
search on arrayData to find the parameter value. It returns True if it was found and
False if it was not found.
Write program code for the function linearSearch().
Save your program.
Copy and paste the program code into part 2(b)(i) in the evidence document.
[6]
(ii)
Edit the main program to:
•
allow the user to input an integer value
•
pass the value to linearSearch() as the parameter
•
output an appropriate message to tell the user whether the search value was found
or not.
Save your program.
Copy and paste the program code into part 2(b)(ii) in the evidence document.
[4]
(iii)
Test your program with one value that is in the array and one value that is not in the
array.
Take a screenshot to show the result of each test.
Save your program.
Copy and paste the screenshots into part 2(b)(iii) in the evidence document.
[2]
(c) The following bubble sort pseudocode algorithm sorts the data in theArray into descending
numerical order. There are five incomplete statements.
PROCEDURE bubbleSort()
DECLARE temp : INTEGER
0 to …
FOR x
0 to …
FOR y
IF theArray[y] … theArray[y + 1] THEN
theArray[y]
temp
…
theArray[y]
…
theArray[y + 1]
ENDIF
NEXT y
NEXT x
ENDPROCEDURE
Write program code for the procedure bubbleSort() to sort the data in arrayData into
descending order.
Save your program.
Copy and paste the program code into part 2(c) in the evidence document.
[6]

Advertisement