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

162 questions · page 6 of 11

0 / 0Answer a question to start your score.
Quick quiz (10)Saved 0
Advertisement
76
9618/42/O/N/24 · Question 3Oct/Nov 2024Paper 4217 marks
Loading question…
Question text

The text file HighScoreTable.txt stores the top seven player scores for a game.
3
The data in the file is stored in the order:
Player ID
Game level
Score
Each item of data is stored on a new line. For example, the first set of data in the file is:
Player ID:
GHEH
Game level:
3
Score:
10
(a) The data about the players and their scores is stored in a 2D array of strings with the identifier
HighScores.
The first dimension of the array has seven elements: one for each player. The second
dimension of the array has three elements: one for the player ID, one for the game level and
one for the score. All data is stored in the array as strings.
HighScores is declared local to the main program, and all elements are initialised to an
empty string, for example " ".
Write program code to declare and initialise HighScores.
Save your program as Question3_N24.
Copy and paste the program code into part 3(a) in the evidence document.
[2]
(b) The function ReadData() reads the data from HighScoreTable.txt and stores the data
in a 2D array. The function returns the 2D array.
The function uses exception handling when opening and reading data from the file.
Write program code for ReadData().
Save your program.
Copy and paste the program code into part 3(b) in the evidence document.
[5]
(c) The procedure OutputHighScores() takes a 2D array as a parameter. It outputs each
player’s ID, level and score in the order they are in the array. The outputs are in the format:
GHEH reached level 3 with a score of 10
Write program code for OutputHighScores().
Save your program.
Copy and paste the program code into part 3(c) in the evidence document.
[2]
(d) The scores in HighScoreTable.txt are not in order.
The player scores are first sorted by the game level they reached. For example, all players
that reached level 5 are higher in the high score table than players that reached level 4.
The players that reached the same level are then sorted in descending order of score.
An example sorted high score table is:
Position
Player ID
Game level
Score
1
GFED
5
25
2
HJKM
4
21
3
RERR
4
19
4
TTYU
4
15
5
WSVG
3
20
6
PPTR
3
15
7
SNQT
2
10
The function SortScores() uses a bubble sort to sort the data as described and returns the
sorted array.
Write program code for SortScores().
Save your program.
Copy and paste the program code into part 3(d) in the evidence document.
[4]
(e) (i)
Amend the main program to implement these steps in the order given:
•
call ReadData() and store the returned array in HighScores
•
output "Before"
•
call OutputHighScores() with HighScores as a parameter
•
call SortScores() and store the returned array in HighScores
•
output "After"
•
call OutputHighScores() with HighScores as a parameter.
Save your program.
Copy and paste the program code into part 3(e)(i) in the evidence document.
[2]
(ii)
Test your program
Take a screenshot of the output.
Save your program.
Copy and paste the screenshot into part 3(e)(ii) in the evidence document.
[2]
Permission to reproduce items where third-party owned material protected by copyright is included has been sought and cleared where possible. Every
publisher will be pleased to make amends at the earliest possible opportunity.
To avoid the issue of disclosure of answer-related information to candidates, all copyright acknowledgements are reproduced online in the Cambridge
Assessment International Education Copyright Acknowledgements Booklet. This is produced for each series of examinations and is freely available to download
at www.cambridgeinternational.org after the live examination series.
Cambridge Assessment International Education is part of Cambridge Assessment. Cambridge Assessment is the brand name of the University of Cambridge

77
9618/21/M/J/24 · Question 3May/June 2024Paper 218 marks
Loading question…
Question text

3
The diagram shows an Abstract Data Type (ADT) representation of a linked list after data items
have been added.
•
PS is the start pointer.
•
PF is the free list pointer.
•
Labels Df, Dc, Db and Dy represent the data items of nodes in the list.
•
Labels Fg, Fh, Fm and Fw represent the data items of nodes in the free list.
•
The symbol Ø represents a null pointer.
PS
Df
Dc
Db
Dy
Ø
PF
Fg
Fh
Fm
Fw
Ø
(a) Describe the linked list immediately after initialisation, before any data items are added.
…
…
…
…
…
… [3]
(b) A program will be written to include a linked list to store alphanumeric user IDs.
The design uses two variables and two 1D arrays to implement the linked list.
Each array element contains data of a single data type and not a record.
The statements below describe the design.
Complete the statements.
The two variables will be of type … .
The two variables will be used as … to the arrays.
The values stored in the two variables will indicate …
… .
The first 1D array will be of type … .
The first 1D array will be used to … .
The second 1D array will be of type … .
The second 1D array will be used to … .
[5]

78
9618/21/M/J/24 · Question 4May/June 2024Paper 216 marks
Loading question…
Question text

A global 1D array Data contains 100 elements of type integer.
4
A function Check() will:
•
total the element values in odd index locations (1, 3, 5 ... 97, 99)
•
total the element values in even index locations (2, 4, 6 ... 98, 100)
•
return one of three strings ‘Odd’, ‘Even’ or ‘Same’ to indicate which total is the greater, or
whether the totals are the same.
Write pseudocode for the function Check().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [6]

Advertisement
79
9618/21/M/J/24 · Question 7May/June 2024Paper 2112 marks
Loading question…
Question text

7
A fitness club has a computerised membership system. The fitness club offers a number of
different exercise classes.
The following information is stored for each club member: name, home address, email address,
mobile phone number, date of birth and the exercise(s) they are interested in.
(a) When an exercise class is planned, a new module will send personalised text messages to
each member who has expressed an interest in that exercise. Members wishing to join the
class send a text message back. Members may decide not to receive future text messages
by replying with the message ‘STOP’.
The process of abstraction is used to filter out unnecessary information.
(i)
State one advantage of applying abstraction to this problem.
…
… [1]
(ii)
Identify three items of information that will be required by the new module.
Justify your choices with reference to the given scenario.
Item 1 required …
Justification …
…
Item 2 required …
Justification …
…
Item 3 required …
Justification …
…
[3]
(iii)
Identify two operations that would be required to process data when the new module
receives a text message back from a member.
Operation 1 …
…
Operation 2 …
…
[2]
(b) The structure chart illustrates part of the membership program:
Update
A
P2
T1
Name
P1
Sub-A
Sub-B
Sub-C
Data item notes:
•
Name contains the name of a club member
•
P1 and T1 are of type real.
(i)
Explain the meaning of the diamond symbol (labelled with the letter A) in the chart.
…
…
…
… [2]
(ii)
Write the pseudocode module headers for Sub-A and Sub-B.
Sub-A
…

80
9618/22/M/J/24 · Question 1May/June 2024Paper 2210 marks
Loading question…
Question text

1
(a) The following table contains pseudocode examples.
Each example may contain statements that relate to one or more of the following:
•
selection
•
iteration (repetition)
•
input/output.
Complete the table by placing one or more ticks (3) in each row.
Pseudocode example
Selection
Iteration
Input/Output
FOR Index
1 TO 10
0
Data[Index]
NEXT Index
WRITEFILE ThisFile, "****"
UNTIL Level > 25
IF Mark > 74 THEN
READFILE OldFile, Data
ENDIF
[4]
(b) Program variables have data types as follows:
Variable
Data type
MyChar
CHAR
MyString
STRING
MyInt
INTEGER
Complete the table by filling in each gap with a function (from the insert) so that each expression
is valid.
Expression
MyInt
… (3.1415926)
MyChar
… ("Elwood", 3, 1)
MyString
… ( … (27.509))
MyInt
… ( … ("ABC123", 3))
[4]
(c) The variables given in part (b) are chosen during the design stage of the program development
life cycle.
The choices are to be documented to simplify program maintenance.
State a suitable way of documenting the variables and give one piece of information that
should be recorded, in addition to the data type.
…
…
… [2]

81
9618/22/M/J/24 · Question 3May/June 2024Paper 229 marks
Loading question…
Question text

3
A factory needs a program to help manage its production of items.
Data will be stored about each item.
The data for each item will be held in a record structure of type Component.
The programmer has started to define the fields that will be needed as shown in the table.
Field
Example value
Comment
Item_Num
123478
a numeric value used as an array index
Reject
FALSE
TRUE if this item has been rejected
Stage
'B'
a letter to indicate the stage of production
Limit_1
13.5
any value in the range 0 to 100 inclusive
Limit_2
26.4
any value in the range 0 to 100 inclusive
Write pseudocode to declare the record structure for type Component.
(a) (i)
…
…
…
…
…
…
… [4]
A 1D array Item of 2000 elements will store the data for all items.
(ii)
Write pseudocode to declare the Item array.
…
… [2]
(b) State three benefits of using an array of records to store the data for all items.
1 …
…
2 …
…
3 …
…
[3]

82
9618/23/M/J/24 · Question 2May/June 2024Paper 237 marks
Loading question…
Question text

2
(a) A program uses a global 1D array of type string and a text file.
An algorithm that forms part of the program is expressed as follows:
•
copy the first line from the file into the first element of the array
•
copy the second line from the file into the second element of the array
•
continue until all lines in the file have been copied into the array.
Stepwise refinement is applied to the algorithm.
Outline five steps for this algorithm that could be used to produce pseudocode.
Assume there are more elements in the array than lines in the file.
Do not use pseudocode statements in your answer.
Step 1 …
…
Step 2 …
…
Step 3 …
…
Step 4 …
…
Step 5 …
…
[5]
(b) Sequence is one programming construct.
Identify one other programming construct that will be required when the algorithm from
part (a) is converted into pseudocode and explain its use.
Construct …
…
Use …
…
[2]

83
9618/23/M/J/24 · Question 3May/June 2024Paper 239 marks
Loading question…
Question text

3
A record structure is declared to hold data relating to components being produced in a factory:
TYPE Component
DECLARE Item_ID : STRING
DECLARE Reject : BOOLEAN
DECLARE Weight : REAL
ENDTYPE
The factory normally produces a batch (or set) of 1000 components at a time. A global array is
declared to store 1000 records for a batch:
DECLARE Batch : ARRAY [1:1000] OF Component
Two global variables contain the minimum and maximum acceptable weight for each component.
The values represent an inclusive range and are declared as:
DECLARE Min, Max : REAL
(a) (i)
A program uses a variable ThisIndex as the array index to access a record.
Write a pseudocode clause to check whether or not the weight of an individual component
is within the acceptable range.
…
…
… [3]
(ii)
When batches of less than 1000 components are processed, it is necessary to indicate
that certain elements in the array are unused.
Suggest how an unused array element could be indicated.
…
… [1]
(b) A module InRange() will:
•
be called with an integer parameter representing an index value of a record in the Batch
array
•
check if the weight of the indexed component is within the acceptable range
•
return TRUE if the weight is in the range and FALSE if it is not.
A module BatchCheck() will:
•
iterate through a batch of 1000 component records
•
call module InRange() to check each individual component record
•
keep a count of the number of components that fail
•
output a suitable warning message and immediately stop if the number of failed
components exceeds 5.
,
,
Complete the program flowchart to represent the algorithm for module BatchCheck().
START
Is
Index = 1001 ?
Yes
No
END
[5]

Advertisement
84
9618/23/M/J/24 · Question 8May/June 2024Paper 2315 marks
Loading question…
Question text

8
A teacher is designing a program to process pseudocode projects written by her students.
The program analyses a student project and extracts information about each module that is
defined (each procedure or function). This information is stored in a global 2D array ModInfo of
type string.
A module header is the first line of a module definition and starts with either of the keywords
PROCEDURE or FUNCTION.
An example of part of the array is given below. Row 10 of the array shows that a procedure header
occurs on line 27 and row 11 shows that a function header occurs on line 35. "P" represents a
procedure and "F" represents a function:
x = 1
x = 2
x = 3
ModInfo[10, x]
"27"
"P"
"MyProc(Z : CHAR)"
ModInfo[11, x]
"35"
"F"
"MyFun(Y : CHAR) RETURNS BOOLEAN"
The string stored in column 3 is called the module description. This is the module header without
the keyword.
A valid module header will:
•
be at least 13 characters long
•
start with the keyword PROCEDURE or FUNCTION. The keyword may appear in either upper or
lower case (or a mix of both) and must be followed by a space character.
The teacher has defined the first program module as follows:
Module
Description
Header()
•
called with a parameter of type string representing a line of
pseudocode
•
if the line is a valid procedure header, returns a string:
"P<Module description>"
•
if the line is a valid function header, returns a string:
"F<Module description>"
•
otherwise, returns an empty string
For example, given the string:
"FUNCTION Zap(X : INTEGER) RETURNS CHAR"
Header()returns the string:
"FZap(X : INTEGER) RETURNS CHAR"
,
,
(a) Write pseudocode for module Header().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…

85
9618/41/M/J/24 · Question 1May/June 2024Paper 4127 marks
Loading question…
Question text

1
A program needs to take integer numbers as input, sort the numbers and then search for a specific
number.
(a) The integer numbers will be stored in the global 1D array, DataStored, with space for up to
20 integers.
The global variable NumberItems stores the quantity of items the array contains.
Write program code to declare DataStored and NumberItems.
Save your program as Question1_J24.
Copy and paste the program code into part 1(a) in the evidence document.
[1]
(b) The procedure Initialise():
•
prompts the user to input the quantity of numbers the user would like to enter
•
reads the input and validates it is between 1 and 20 (inclusive)
•
prompts the user to input each number and stores each number in DataStored.
Write program code for Initialise().
Save your program.
Copy and paste the program code into part 1(b) in the evidence document.
[5]
(c) The main program stores 0 in NumberItems, calls Initialise() and then outputs the
contents of DataStored.
(i)
Write program code for the main program.
Save your program.
Copy and paste the program code into part 1(c)(i) in the evidence document.
[2]
(ii)
Test your program by inputting the following data in the order given:
30
5
3
9
4
1
2
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 1(c)(ii) in the evidence document.
[2]
(d) The procedure BubbleSort() uses a bubble sort to sort the data in DataStored into
ascending numerical order.
Write program code for BubbleSort().
(i)
Save your program.
Copy and paste the program code into part 1(d)(i) in the evidence document.
[4]
Write program code to amend the main program to call BubbleSort() and then output
(ii)
the contents of DataStored.
Save your program.
Copy and paste the program code into part 1(d)(ii) in the evidence document.
[1]
(iii)
Test your program by inputting the following data in the order given:
5
3
9
4
1
2
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 1(d)(iii) in the evidence document.
[1]
(e) The function BinarySearch():
•
takes the integer parameter DataToFind to search for in the array
•
performs an iterative binary search on the array DataStored
•
returns the index where DataToFind is found in DataStored. If DataToFind is not
found, the function returns −1.
Write program code for the iterative function BinarySearch().
(i)
Save your program.
Copy and paste the program code into part 1(e)(i) in the evidence document.
[6]
(ii)
Write program code to amend the main program to:
•
take a number as input from the user
•
call BinarySearch() with the number input
•
output the value returned from the function call as its parameter.
Save your program.
Copy and paste the program code into part 1(e)(ii) in the evidence document.
[3]
(iii)
Test your program twice with the following inputs:
Test 1: 5
1
6
2
8
10
2
Test 2: 5
1
6
2
8
10
7
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 1(e)(iii) in the evidence document.
[2]

86
9618/41/M/J/24 · Question 3May/June 2024Paper 4117 marks
Loading question…
Question text

3
A program reads data from the user and stores the data that is valid in a linear queue.
The queue is stored as a global 1D array, QueueData, of string values. The array needs space for
20 elements.
The global variable QueueHead stores the index of the first element in the queue.
The global variable QueueTail stores the index of the last element in the queue.
(a) The main program initialises all the elements in QueueData to a suitable null value,
QueueHead to −1 and QueueTail to −1.
Write program code for the main program.
Save your program as Question3_J24.
Copy and paste the program code into part 3(a) in the evidence document.
[1]
(b) The function Enqueue() takes the data to insert into the queue as a parameter.
If the queue is not full, it inserts the parameter in the queue, updates the appropriate pointer(s)
and returns TRUE. If the queue is full, it returns FALSE.
Write program code for Enqueue().
Save your program.
Copy and paste the program code into part 3(b) in the evidence document.
[4]
(c) The function Dequeue() returns "false" if the queue is empty. If the queue is not empty, it
returns the next item in the queue and updates the appropriate pointer(s).
Write program code for Dequeue().
Save your program.
Copy and paste the program code into part 3(c) in the evidence document.
[3]
(d) The string values to be stored in the queue are 7 characters long. The first 6 characters are
digits and the 7th character is a check digit. The check digit is calculated from the first 6 digits
using this algorithm:
•
multiply the digits in position 0, position 2 and position 4 by 1
•
multiply the digits in position 1, position 3 and position 5 by 3
•
calculate the sum of the products (add together the results from all of the multiplications)
•
divide the sum of the products by 10 and round the result down to the nearest integer to
get the check digit
•
if the check digit equals 10 then it is replaced with 'X'.
Example:
Data is 954123
Character position
0
1
2
3
4
5
Digit
9
5
4
1
2
3
Multiplier
1
3
1
3
1
3
Product
9
15
4
3
2
9
Sum of products = 9 + 15 + 4 + 3 + 2 + 9 = 42
Divide sum of products by 10: 42 / 10 = 4 (rounded down)
The check digit = 4. This is inserted into character position 6.
The data including the check digit is: 9541234
A 7‑character string is valid if the 7th character matches the check digit for that data. For
example, the data 9541235 is invalid because the 7th character (5) does not match the check
digit for 954123.
The subroutine StoreItems() takes ten 7‑character strings as input from the user and
(i)
uses the check digit to validate each input.
Each valid input has the check digit removed and is stored in the queue using
Enqueue().
An appropriate message is output if the item is inserted. An appropriate message is
output if the queue is already full.
Invalid inputs are not stored in the queue.
The subroutine counts and outputs the number of invalid items that were entered.
StoreItems() can be a procedure or a function as appropriate.
Write program code for StoreItems().
Save your program.
Copy and paste the program code into part 3(d)(i) in the evidence document.
[6]
(ii)
Write program code to amend the main program to:
•
call StoreItems()
•
call Dequeue()
•
output a suitable message if the queue was empty
•
output the returned value if the queue was not empty.
Save your program.
Copy and paste the program code into part 3(d)(ii) in the evidence document.
[1]
(iii)
Test the program with the following inputs in the order given:
999999X
1251484
5500212
0033585
9845788
6666666
3258746
8111022
7568557
0012353
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 3(d)(iii) in the evidence document.
[2]
Permission to reproduce items where third‑party owned material protected by copyright is included has been sought and cleared where possible. Every
publisher will be pleased to make amends at the earliest poss

87
9618/42/M/J/24 · Question 1May/June 2024Paper 4222 marks
Loading question…
Question text

1
A program outputs a main word. The program asks the user to enter the different words of 3 or
more letters that can be made from the letters in the main word. These are called the answers.
There are 3 files: Easy.txt, Medium.txt and Hard.txt. Each file has the main word on the
first line. For example, the main word in Easy.txt is house.
The answers are stored in the file. Each answer is on a new line after the main word. For example,
Easy.txt has 14 answers that can be made from the letters in house.
The words read from the text file are stored in the global array WordArray. The number of words
that can be made from the letters in the main word is stored in the global variable NumberWords.
(a) The procedure ReadWords():
•
takes a file name as a parameter
•
opens the file and reads in the data
•
stores the main word in the first element in WordArray
•
stores each answer in a new element in WordArray
•
counts and stores the number of answers.
Write program code for the procedure ReadWords().
Save your program as Question1_J24.
Copy and paste the program code into part 1(a) in the evidence document.
[6]
(b) The main program asks the user to enter "easy", "medium" or "hard" and calls
ReadWords()with the filename that matches the user’s input. For example, if the user enters
"easy", the parameter is "Easy.txt".
Write program code for the main program.
Save your program.
Copy and paste the program code into part 1(b) in the evidence document.
[4]
The procedure Play():
(c) (i)
•
outputs the main word from the array and the number of answers
•
allows the user to enter words until they enter the word ‘no’ to indicate they want to
stop
•
outputs whether each word the user enters is an answer or not an answer
•
counts the number of answers the user gets correct
•
replaces each answer that the user gets correct with a null value in the array.
Write program code for the procedure Play().
Save your program.
Copy and paste the program code into part 1(c)(i) in the evidence document.
[6]
Amend the procedure Play()so that when the user enters the command to stop, the
(ii)
procedure:
•
outputs the percentage of answers the user entered from the array
•
outputs all the answers that the user did not enter.
Write program code to amend Play().
Save your program.
Copy and paste the program code into part 1(c)(ii) in the evidence document.
[3]
The procedure ReadWords()calls Play()after the data in the file has been read.
(d) (i)
Write program code to amend ReadWords().
Save your program.
Copy and paste the program code into part 1(d)(i) in the evidence document.
[1]
(ii)
Test your program by inputting these words in the order shown:
easy
she
out
no
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 1(d)(ii) in the evidence document.
[1]
(iii)
Test your program by inputting these words in the order shown:
hard
fine
fined
idea
no
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 1(d)(iii) in the evidence document.
[1]

88
9618/42/M/J/24 · Question 2May/June 2024Paper 4230 marks
Loading question…
Question text

2
A binary tree stores data in ascending order. For example:
15
8
19
3
10
A computer program stores integers in a binary tree in ascending order. The program uses
Object‑Oriented Programming (OOP).
The binary tree is stored as a 1D array of nodes. Each node contains a left pointer, a data value
and a right pointer.
The class Node stores the data about a node.
Node
LeftPointer : INTEGER
stores the index of the node to the left in the binary tree
Data : INTEGER
stores the node’s data
RightPointer : INTEGER
stores the index of the node to the right in the binary tree
Constructor()
initialises Data to its parameter value
initialises LeftPointer and RightPointer to −1
GetLeft()
returns the left pointer
GetRight()
returns the right pointer
GetData()
returns the data value
SetLeft()
assigns the parameter to the left pointer
SetRight()
assigns the parameter to the right pointer
SetData()
assigns the parameter to the data
Write program code to declare the class Node and its constructor.
(a) (i)
Do not declare the other methods.
Use the appropriate constructor for your programming language.
If you are writing in Python, include attribute declarations using comments.
Save your program as Question2_J24.
Copy and paste the program code into part 2(a)(i) in the evidence document.
[4]
The get methods GetLeft(), GetRight()and GetData()each return the relevant
(ii)
attribute.
Write program code for the three get methods.
Save your program.
Copy and paste the program code into part 2(a)(ii) in the evidence document.
[3]
The set methods SetLeft(), SetRight()and SetData()each take a parameter and
(iii)
then store this in the relevant attribute.
Write program code for the three set methods.
Save your program.
Copy and paste the program code into part 2(a)(iii) in the evidence document.
[3]
(b) The class TreeClass stores the data about the binary tree.
TreeClass
Tree[0:19] : Node
an array of 20 elements of type Node
FirstNode : INTEGER
stores the index of the first node in the tree
NumberNodes : INTEGER
stores the quantity of nodes in the tree
Constructor()
initialises FirstNode to −1 and NumberNodes to 0
initialises each element in Tree to a Node object with the
data value of −1
InsertNode()
takes a Node object as a parameter, inserts it in the array
and updates the pointer for its parent node
OutputTree()
outputs the left pointer, data and right pointer of each
node in Tree
Nodes cannot be deleted from this tree.
Write program code to declare the class TreeClass and its constructor.
(i)
Do not declare the other methods.
Use the appropriate constructor for your programming language.
If you are writing in Python, include attribute declarations using comments.
Save your program.
Copy and paste the program code into part 2(b)(i) in the evidence document.
[4]
The method InsertNode()takes a Node object, NewNode, as a parameter and inserts
(ii)
it into the array Tree.
InsertNode()first checks if the tree is empty.
If the tree is empty, InsertNode():
•
stores NewNode in the array Tree at index NumberNodes
•
increments NumberNodes
•
stores 0 in FirstNode.
If the tree is not empty, InsertNode():
•
stores NewNode in the array Tree at index NumberNodes
•
accesses the data in the array Tree at index FirstNode and compares it to the
data in NewNode
•
repeatedly follows the pointers until the correct position for NewNode is found
•
once the position is found, InsertNode()sets the left or right pointer of its parent
node
•
increments NumberNodes.
Write program code for InsertNode().
Save your program.
Copy and paste the program code into part 2(b)(ii) in the evidence document.
[6]
The method OutputTree()outputs the left pointer, the data and the right pointer for
(iii)
each node that has been inserted into the tree. The outputs are in the order they are
saved in the array.
If there are no nodes in the array, the procedure outputs ‘No nodes’.
Write program code for OutputTree().
Save your program.
Copy and paste the progra

Advertisement
89
9618/42/M/J/24 · Question 3May/June 2024Paper 4223 marks
Loading question…
Question text

3
A program sorts an array of integers and searches the array for a particular value.
(a) The array of integers, NumberArray, stores the following data in the order given:
100
85
644
22
15
8
1
The array is declared and initialised local to the main program.
Write program code to declare and initialise the array.
Save your program as Question3_J24.
Copy and paste the program code into part 3(a) in the evidence document.
[1]
(b) (i)
The following recursive pseudocode function sorts the array into ascending order using
an insertion sort and returns the sorted array.
DECLARE LastItem : INTEGER
DECLARE CheckItem : INTEGER
DECLARE LoopAgain : BOOLEAN
FUNCTION RecursiveInsertion(IntegerArray : ARRAY[] OF INTEGER,
NumberElements : INTEGER) RETURNS ARRAY[] OF INTEGER
IF NumberElements <= 1 THEN
RETURN IntegerArray
ELSE
CALL RecursiveInsertion(IntegerArray, NumberElements - 1)
LastItem
IntegerArray[NumberElements - 1]
CheckItem
NumberElements - 2
ENDIF
LoopAgain
TRUE
IF CheckItem < 0 THEN
LoopAgain
FALSE
ELSE
IF IntegerArray[CheckItem] < LastItem THEN
LoopAgain
FALSE
ENDIF
ENDIF
WHILE LoopAgain
IntegerArray[CheckItem + 1]
IntegerArray[CheckItem]
CheckItem
CheckItem - 1
IF CheckItem < 0 THEN
LoopAgain
FALSE
ELSE
IF IntegerArray[CheckItem] < LastItem THEN
LoopAgain
FALSE
ENDIF
ENDIF
ENDWHILE
IntegerArray[CheckItem + 1]
LastItem
RETURN IntegerArray
ENDFUNCTION
Write the program code for the pseudocode function RecursiveInsertion().
Save your program.
Copy and paste the program code into part 3(b)(i) in the evidence document.
[4]
(ii)
Amend the main program to:
•
call RecursiveInsertion()with the initialised array NumberArray and the
number of elements as parameters
•
output the word ‘Recursive’
•
output the content of the returned array.
Save your program.
Copy and paste the program code into part 3(b)(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 part 3(b)(iii) in the evidence document.
[1]
(c) The function RecursiveInsertion()can be changed to use iteration instead of recursion.
Write program code for the function IterativeInsertion()to perform the same
(i)
processes as RecursiveInsertion()but using iteration instead of recursion.
Save your program.
Copy and paste the program code into part 3(c)(i) in the evidence document.
[4]
(ii)
Write program code to amend the main program to also:
•
call IterativeInsertion()with the original initialised array NumberArray
•
output the word ‘iterative’
•
output the content of the returned array.
Save your program.
Copy and paste the program code into part 3(c)(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 part 3(c)(iii) in the evidence document.
[1]
(d) The recursive function BinarySearch()takes the parameters:
•
IntegerArray – an array of integers
•
First – the index of the first array element
•
Last – the index of the last array element
•
ToFind – an integer to search for in the array.
The function uses recursion to perform a binary search for ToFind in IntegerArray.
The function returns the index where ToFind is stored or returns −1 if ToFind is not in the
array.
Write program code for the recursive function BinarySearch().
(i)
Save your program.
Copy and paste the program code into part 3(d)(i) in the evidence document.
[6]
(ii)
Write program code to amend the main program to:
•
call BinarySearch()with the sorted array and the integer 644 as the search value
•
output ‘Not found’ if 644 is not found in the array
•
output the index if 644 is found in the array.
Save your program.
Copy and paste the program code into part 3(d)(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 part 3(d)(iii) in the evidence document.
[1]

90
9618/21/O/N/23 · Question 1Oct/Nov 2023Paper 2111 marks
Loading question…
Question text

1
The following pseudocode represents part of the algorithm for a program:
CASE OF ThisValue
←
"Low"
// less than 30
< 30
: Level
←
1
Check
←
"Very Low"
// less than 20
< 20
: Level
←
ThisValue / 2
Check
←
"Medium"
// between 30 and 40
30 TO 40 : Level
←
ThisValue / 3
Check
←
Data[ThisValue] + 1
Data[ThisValue]
←
"High"
> 40
: Level
ENDCASE
(a) Complete the table by writing the answer for each row:
Answer
The value assigned to Level when ThisValue is 40
The value assigned to Check when ThisValue is 36
The value assigned to Level when ThisValue is 18
The number of elements in array Data that may be incremented
[4]
(b) The pseudocode contains four assignments to variable Level. One of these assignments
will never be performed.
Identify this assignment and explain why this is the case.
…
…
…
…
…
… [3]
(c) The following line is added immediately before the ENDCASE statement:
← "Undefined"
OTHERWISE : Level
State why this assignment is never performed.
…
… [1]
(d) Give the appropriate data types for the variables ThisValue, Check and Level.
ThisValue …
Check …
Level …
[3]

Advertisement