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

162 questions · page 5 of 11

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

3
An algorithm will output the last three lines from a text file Result.txt
The lines need to be output in the same order as they appear in the file.
Assume:
•
Three variables LineX, LineY and LineZ will store the three lines. These are of type string
and all three variables have been initialised to an empty string.
•
The file exists and contains at least three lines.
(a) The algorithm to output the lines is expressed in eight steps.
Complete the steps.
1.
Open the file …
2.
Loop until …
3.
… and store in ThisLine
4.
Assign LineY to LineX
5.
Assign LineZ to LineY
6.
Assign ThisLine to LineZ
7.
After the loop, …
8.
Output LineX, LineY, LineZ
[4]
(b) Explain the purpose of steps 4, 5 and 6 in the algorithm from part (a).
…
…
… [1]
ĬÙĊ®Ġ´íÈõÏĪÅĊÝú¶þ×
,
,
(c) The requirement changes, and the algorithm will now output three lines from the file, starting
from a given line number.
The modified algorithm will be implemented as a function which will:
•
be called with an integer parameter representing the given line number
•
output three lines, starting at the given line
•
return TRUE if the 3 lines are output, or FALSE if it was not possible to output the 3 lines.
Describe the changes that need to be made to steps 2 to 8 of the algorithm given in part (a).
…
…
…
…
…
…
…
…
…
… [4]
ĬÛĊ®Ġ´íÈõÏĪÅĊÝü¶þ×

62
9618/21/O/N/24 · Question 6Oct/Nov 2024Paper 219 marks
Loading question…
Question text

6
A factory produces food items. The items must be used within a certain number of days after their
production date. The number of days is known as the shelf life. It is different for each type of item
but is always a whole number in the range 1 to 21 (inclusive).
The latest date that an item can be used is called the ‘use-by’ date.
A program is needed to produce labels which show the ‘use-by’ date.
Part of the program is a function GetDate() which will:
•
take two parameters: a production date and a value representing the shelf life
•
return the corresponding ‘use-by’ date.
The program contains a global 1D array DaysInMonth of type integer which stores the number of
days in each month (index 1 is January):
Index
Value
31
1
28
2
Note: Leap years are not considered
31
3
30
4
30
11
31
12
(a) An algorithm uses the array DaysInMonth to calculate a ‘use-by’ date. An alternative design
would involve the use of multiple selection statements.
An array-based technique is often used when there is a large number of different values to
check and where no pattern exists.
One advantage of using an array-based technique is the speed of execution compared to the
use of multiple selection statements.
Give two other advantages of using an array for this type of operation rather than a solution
based on multiple selection statements.
1 …
…
2 …
…
[2]
ĬÙĊ®Ġ´íÈõÏĪÅĊàú¸Ā×
,
,
(b) Complete the pseudocode for the function GetDate().
Date functions from the insert should be used in your solution.
FUNCTION GetDate(ProductionDate : DATE, ShelfLife : INTEGER) RETURNS DATE
…
…
…
…
…
…
…
…
…
…
…
…

63
9618/21/O/N/24 · Question 8Oct/Nov 2024Paper 2130 marks
Loading question…
Question text

8
An exam paper has a maximum of 75 marks. One of five pass grades (A to E) is assigned,
depending on the mark obtained. The lowest mark for a given grade is known as the grade
boundary. For example, if the grade boundary for an A grade is 65 marks, then any candidate who
achieves a mark of 65 or above will be awarded an A. A grade of U is awarded for marks below the
E grade boundary.
The five grade boundaries are stored in a global 1D array GradeBoundary of type integer.
For example:
Element
Value
Comment
GradeBoundary[1]
65
The minimum mark for an A grade.
GradeBoundary[2]
57
The minimum mark for a B grade.
GradeBoundary[3]
43
The minimum mark for a C grade.
GradeBoundary[4]
35
The minimum mark for a D grade.
GradeBoundary[5]
27
The minimum mark for an E grade.
A global 2D array Result of type integer contains candidate marks for the exam. Each row
relates to one candidate. Column 1 contains the candidate mark and column 2 contains the unique
candidate ID.
For example, for the fourth and fifth candidates:
Element
Mark
Element
ID
Result[4, 1]
56
Result[4, 2]
1074832
Result[5, 1]
54
Result[5, 2]
2573839
There are more rows in the array than candidates who sit the exam. Any unused rows will be at
the end of the array.
Candidate papers that are given a mark within two marks of any grade boundary must be checked.
For example, given the values in the example grade boundaries above, any paper that was
awarded between 41 and 45 marks (inclusive) would need to be checked.
A program is being written to identify papers that need to be checked.
The programmer has defined the first program module as follows:
Module
Description
•
called with a parameter of type integer representing a
candidate mark
CheckMark()
•
returns TRUE if the mark is within 2 of any of the five grade
boundaries, otherwise returns FALSE
ĬÕĊ®Ġ´íÈõÏĪÅĊÝûµĄ×
,
,
(a) Write pseudocode for module CheckMark().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…

Advertisement
64
9618/22/O/N/24 · Question 1Oct/Nov 2024Paper 229 marks
Loading question…
Question text

1
A program has been developed and released for general use. After a few months of use an error is
detected where under certain circumstances it outputs an unexpected value.
(a) The error in the program needs to be corrected.
Identify the stage of the program development life cycle that this correction is made in.
… [1]
(b) The program contains a function Lookup(). After investigation, it is found that this is the
function that sometimes returns an incorrect value.
An Integrated Development Environment (IDE) is used to help locate the error.
The IDE features of watch window, single stepping and breakpoint will be used.
Explain these features including the order that they will be used in to locate the error in
Lookup().
…
…
…
…
…
… [3]
(c) To solve the error a programmer decides to create a new module.
The design of the new module has been completed and the module is being coded.
Identify two features of an IDE that will help during the coding of this new module.
1 …
2 …
[2]
(d) The new module referred to in part (c) introduces three new variables.
Complete the following table by giving the appropriate data type for each.
Variable name
Used to store
Data type
Name
A customer name.
Index
An array index.
The result of the division of any two
Result
non-zero numbers.
[3]
ĬÍĊ¾Ġ´íÈõÏĪÅĊÝú¸þ×

65
9618/22/O/N/24 · Question 2Oct/Nov 2024Paper 229 marks
Loading question…
Question text

2
A program is being developed to process bank card information.
When a card number is displayed, all the characters except the last four are replaced with the
asterisk character '*'.
Card numbers are stored as strings. The strings are between 10 and 20 characters in length.
The function Conceal() will take a string representing a card number and return a modified
string.
Example strings:
Original string
Modified string
"1234567890"
"******7890"
"1234567897652"
"*********7652"
"1234567890123456" "************3456"
(a) The function Conceal() will:
•
take a numeric string as a parameter representing the card number
•
return a string in which the asterisk character replaces all except the last four characters
of the card number parameter.
Write pseudocode for the function Conceal().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [6]
ĬÍĊ¾Ġ´íÈõÏĪÅĊßú¸Ā×
,
,
(b) The requirements have been changed. Conceal() will now be written as a procedure which
will process 100 card numbers each time it is called.
The card numbers will be stored in a 2D array CardNumber. The original string will be stored
in column one and the modified string in column two.
(i)
Write pseudocode to declare the array.
…
… [2]
(ii)
The new procedure Conceal() will write the modified string to the corresponding
element in column two.
The array CardNumber is passed as a parameter to the new procedure Conceal().
Identify how this parameter should be specified in the new procedure header.
…

66
9618/22/O/N/24 · Question 3Oct/Nov 2024Paper 227 marks
Loading question…
Question text

3
A program uses a stack to hold up to 60 numeric values.
The stack is implemented using two integer variables and a 1D array.
The array is declared in pseudocode as shown:
DECLARE ThisStack : ARRAY[1:60] OF REAL
The stack operates as follows:
•
Global variable SP acts as a stack pointer that points to the next available stack location. The
value of SP represents an array index.
•
Global variable OnStack represents the number of values currently on the stack.
•
The stack grows upwards from array element index 1.
(a) (i)
Give the initial values that should be assigned to the two variables.
SP …
OnStack …
[1]
(ii)
Explain why it is not necessary to initialise the array elements before the stack is used.
…
…
…
… [2]
(b) A function to add a value to ThisStack is expressed in pseudocode as shown.
The function will return a value to indicate whether the operation was successful or not.
Complete the pseudocode by filling in the gaps.
FUNCTION Push(ThisValue : REAL) RETURNS BOOLEAN
DECLARE ReturnValue : BOOLEAN
IF … THEN
RETURN … // stack is already full
ENDIF
ThisValue
…
…
SP
OnStack + 1
OnStack
RETURN TRUE
ENDFUNCTION
[4]
ĬÑĊ¾Ġ´íÈõÏĪÅĊÞüµþ×

67
9618/22/O/N/24 · Question 6Oct/Nov 2024Paper 229 marks
Loading question…
Question text

6
A shop sells sandwiches and snacks. The owner chooses a ‘daily special’ sandwich which is
displayed on a board outside the shop. Each ‘daily special’ has two different fillings and is made
with one type of bread.
The owner wants a program to randomly choose the ‘daily special’ sandwich.
The program designer decides to store the possible sandwich fillings in a 1D array of type string.
The array is declared in pseudocode as follows:
DECLARE Filling : ARRAY [1:35] OF STRING
Each element contains the name of one filling.
An example of the first five elements is as follows:
Index
Element value
"Cheese"
1
"Onion"
2
"Salmon"
3
"Anchovies"
4
"Peanut Butter"
5
A second 1D array stores the possible bread used:
DECLARE Bread : ARRAY [1:10] OF STRING
Each element contains the name of one type of bread.
An example of the first three elements is as follows:
Index
Element value
"White"
1
"Brown"
2
"Pitta"
3
Both arrays may contain unused elements. The value of these will be an empty string and they
may occur anywhere in each array.
A procedure Special() will output a message giving the ‘daily special’ sandwich made from two
randomly selected different fillings and one randomly selected bread.
Unused array elements must not be used when creating the ‘daily special’ sandwich.
Using the above examples, the output could be:
"The daily special is Cheese and Onion on Brown bread."
ĬÑĊ¾Ġ´íÈõÏĪÅĊßü·Ā×
,
,
(a) Complete the pseudocode for the procedure Special().
Assume that both arrays are global.
PROCEDURE Special()
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…

68
9618/22/O/N/24 · Question 8Oct/Nov 2024Paper 2218 marks
Loading question…
Question text

8
A program is being developed to implement a game for up to six players.
During the game, each player assembles a team of characters. At the start of the game there are
45 characters available.
Each character has four attributes, as follows:
Attribute
Examples
Comment
Player
0, 1, 3
The player the character is assigned to.
The job that the character will perform in the
Role
Builder, Teacher, Doctor
game.
The name of the character. Several characters
Name
Bill, Lee, Farah, Mo
may perform the same role, but they will each
have a unique name.
The skill level of the character. An integer in the
Level
14, 23, 76
range 0 to 100 inclusive.
The programmer has defined a record type to define each character.
The record type definition is shown in pseudocode as follows:
TYPE CharacterType
DECLARE Player : INTEGER
DECLARE Role : STRING
DECLARE Name : STRING
DECLARE Level : INTEGER
ENDTYPE
The Player field indicates the player to which the character is assigned (1 to 6). The field value is
0 if the character is not assigned to any player.
The programmer has defined a global array to store the character data as follows:
DECLARE Character : ARRAY[1:45] OF CharacterType
At the start of the game all record fields are initialised, and all Player field values are set to 0
The programmer has defined a program module as follows:
Module
Description
Assign()
•
called with two parameters:
○ an integer representing a player
○ a string representing a character role
•
search the Character array for an unassigned character with the
required role
•
If found, assign the character to the given player and output a confirmation
message, for example:
"Bill the Builder has been assigned to player 3"
•
If no unassigned character with the required role is found, output a suitable
message.
ĬÍĊ¾Ġ´íÈõÏĪÅĊßù¸Ă×
,
,
(a) Write pseudocode for module Assign().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…

Advertisement
69
9618/23/O/N/24 · Question 2Oct/Nov 2024Paper 237 marks
Loading question…
Question text

2
An algorithm will:
1.
prompt and input a sequence of 100 integer values, one at a time
2.
sum the positive integers
3.
output the result of the sum.
(a) Write pseudocode for the algorithm.
Assume the value zero is neither positive nor negative.
You must declare all variables used in the algorithm.
…
…
…
…
…
…
…
…
…
…
…
…
… [5]
(b) The algorithm requires the use of basic constructs. One of these is sequence.
Identify one other basic construct required by the algorithm and describe how it is used.
Construct …
Use …
…
[2]
Ĭ×Ċ®Ġ´íÈõÏĪÅĊÝû·þ×

70
9618/23/O/N/24 · Question 3Oct/Nov 2024Paper 2310 marks
Loading question…
Question text

3
The implementation of a linked list uses an integer variable and a 1D array List of type Node.
Record type Node is declared in pseudocode as follows:
TYPE Node
DECLARE Data : STRING
DECLARE Pointer : INTEGER
ENDTYPE
The array List is declared in pseudocode as follows:
DECLARE List : ARRAY[1:200] OF Node
The linked list will operate as follows:
•
Integer variable HeadPointer will store the array index for the first node in the linked list.
•
The Pointer field of a node contains the index value of the next array element (the next
node) in the linked list.
•
The value 0 is used as a null pointer.
(a) (i)
State why the value 0 has been selected as the null pointer.
…
… [1]
(ii)
Give the range of valid values that could be assigned to variable HeadPointer.
… [1]
ĬÕĊ®Ġ´íÈõÏĪÅĊßù·Ā×
,
,
(b) The array List will be initialised so that each node points to the following node.
The last node will contain a null pointer.
Complete the program flowchart to represent the algorithm for this operation.
START
END
[4]
Ĭ×Ċ®Ġ´íÈõÏĪÅĊßû·Ā×
,
,
(c) An algorithm outputs the Data field from all nodes in the array List. The order the Data is
output should be the same order it is stored in the linked list.
Describe the algorithm in four steps.
Do not use pseudocode statements in your answer.
Step 1 …
…
…
Step 2 …
…
…
Step 3 …
…
…
Step 4 …
…
…
[4]
ĬÙĊ®Ġ´íÈõÏĪÅĊÞû¶þ×

71
9618/23/O/N/24 · Question 4Oct/Nov 2024Paper 2312 marks
Loading question…
Question text

4
An examination paper has a maximum of 75 marks. One of five pass grades (A to E) is assigned,
depending on the mark obtained. The lowest mark for a given grade is known as the grade boundary.
A program is being written to process examination marks.
The five grade boundaries are stored in a global 1D array GB of type INTEGER, for example:
Index
Value
Comment
65
1
The minimum mark for an A grade.
57
2
The minimum mark for a B grade.
43
3
The minimum mark for a C grade.
35
4
The minimum mark for a D grade.
27
5
The minimum mark for an E grade.
Any paper that achieves a mark within 2 marks of a grade boundary must be checked. Using the
given table, a paper with 45 marks would need to be checked.
(a) The pseudocode algorithm to determine whether a paper should be checked is as shown.
The mark for the paper is stored in variable Mark. Global variables Mark, Index, Upper and
Lower are declared as integers.
Complete the pseudocode.
← 1 TO …
FOR Index
Lower ← GB[Index] - 2
Upper ← …
IF Mark … AND Mark … THEN
OUTPUT "Check this paper"
ENDIF
NEXT Index
[4]
(b) An alternative algorithm to determine if a paper needs to be checked uses a global 1D array
Check, containing 76 elements of type BOOLEAN. The indices of the array are from 0 to 75
(inclusive), corresponding to the range of possible marks.
An element value in Check is TRUE if the index is within 2 marks of a grade boundary. For
example, in the case where the C grade boundary is 43 the corresponding part of the Check
array would be as follows:
Index
Value
FALSE
40
TRUE
41
TRUE
42
← The grade boundary for a C grade
TRUE
43
TRUE
44
TRUE
45
FALSE
46
ĬÙĊ®Ġ´íÈõÏĪÅĊàû¶Ā×
,
,
(i)
The mark for a given paper is stored in variable Mark.
Describe how an algorithm would use the Check array to determine whether this paper
should be checked.
…
…
…
… [2]
(ii)
A procedure GBInitialise() will initialise the Check array using values from the GB
array.
Note it can be assumed that the maximum grade boundary value for A is 70 and the
minimum value for E is 15.
Write pseudocode for the procedure.
…
…
…
…
…
…
…
…
…

72
9618/23/O/N/24 · Question 8Oct/Nov 2024Paper 2316 marks
Loading question…
Question text

8
A program is being developed to implement a game for up to six players.
During the game, each player assembles a team of characters. At the start of the game there are
45 characters available.
Each character has four attributes, as follows:
Attribute
Examples
Comment
Player
0, 1, 3
The player the character is assigned to.
The job that the character will perform in the
Role
Builder, Teacher, Doctor
game.
The name of the character. Several characters
Name
Bill, Lee, Farah, Mo
may perform the same role, but they will each
have a unique name.
Skill level
14, 23, 76
An integer in the range 0 to 100, inclusive.
The programmer has defined a record type to define each character. The record type definition is
shown in pseudocode as follows:
TYPE CharacterType
DECLARE Player : INTEGER
DECLARE Role : STRING
DECLARE Name : STRING
DECLARE SkillLevel : INTEGER
ENDTYPE
The Player field indicates the player to which the character is assigned (1 to 6). This field value
is 0 if the character is not assigned to any player.
The programmer has defined a global array to store the character data, as follows:
DECLARE Character : ARRAY[1:45] OF CharacterType
At the start of the game all record fields are initialised, and all Player fields are set to 0
The programmer has defined a program module as follows:
Module
Description
Count()
•
called with two parameters:
○ an integer representing a player
○ a string representing a character role
•
searches the Character array for characters with the given role that are
assigned to the given player
•
counts the number of assigned characters and sums their total skill level
•
outputs the result of the search if characters with the given role are found,
for example:
"Player 3 has 4 characters with the role of Teacher
and the total skill level is 65"
•
if no characters with the given role are found, outputs:
"No characters with that role are assigned to this
player"
ĬÕĊ®Ġ´íÈõÏĪÅĊàúµĂ×
,
,
(a) Complete the pseudocode for module Count().
PROCEDURE Count(ThisPlayer : INTEGER, ThisRole : STRING)
…
…
…
…
…
…
…
…
…
…
…
…
…
…

73
9618/41/O/N/24 · Question 1Oct/Nov 2024Paper 4122 marks
Loading question…
Question text

1
A program sorts string data using different sorting methods.
(a) The text file Data.txt stores string data items. Each data item is on a new line in the text
file.
The function ReadData():
•
has a local array of strings that can store 45 items
•
reads each line of data and stores it in the array
•
returns the array.
Write program code for the function ReadData().
Save your program as Question1_N24.
Copy and paste the program code into part 1(a) in the evidence document.
[6]
(b) The function FormatArray() takes an array of strings as a parameter. It concatenates the
contents of the array into one string with a space between each array element. The function
returns the concatenated string.
Write program code for FormatArray().
(i)
Save your program.
Copy and paste the program code into part 1(b)(i) in the evidence document.
[2]
(ii)
The main program:
•
calls ReadData() and stores the returned array
•
calls FormatArray() with the returned array and outputs the returned string.
Write program code for the main program.
Save your program.
Copy and paste the program code into part 1(b)(ii) in the evidence document.
[3]
(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 CompareStrings():
•
takes two strings as parameters
•
compares each string, one character at a time, to identify which string comes first
alphabetically. If the first two characters are the same, the second character of each
string is compared. This continues until the two characters are different.
The function:
•
returns 1 if the first parameter comes before the second alphabetically
•
returns 2 if the second parameter comes before the first alphabetically.
Write program code for CompareStrings().
Assume that all strings are in lower case.
Assume that a difference between two strings will always be identified before the end of one
string is reached.
Do not use an in-built string comparison function.
The strings must be compared one character at a time.
Save your program.
Copy and paste the program code into part 1(c) in the evidence document.
[4]
(d) The function Bubble() takes an array of strings as a parameter and sorts the data into
ascending alphabetical order, using a bubble sort. The bubble sort uses CompareStrings()
to compare each string.
The function returns the sorted list.
Write program code for Bubble().
(i)
Save your program.
Copy and paste the program code into part 1(d)(i) in the evidence document.
[3]
(ii)
Write program code to amend the main program to:
•
call Bubble() with the unsorted array as a parameter
•
call FormatArray() with the sorted array and output the returned string.
Save your program.
Copy and paste the program code into part 1(d)(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(d)(iii) in the evidence document.
[1]

Advertisement
74
9618/41/O/N/24 · Question 3Oct/Nov 2024Paper 4119 marks
Loading question…
Question text

3
A linked list stores positive integer data in a 2D array. The first dimension of the array stores the
integer data. The second dimension of the array stores the pointer to the next node in the linked
list.
A linked list node with no data is initialised with the integer –1. These nodes are linked together as
an empty list. A pointer of –1 identifies that node as the last node.
The linked list can store 20 nodes.
The global 2D array LinkedList stores the linked list.
LinkedList is initialised as an empty list. The data in each node is initialised to –1. Each node’s
pointer stores the index of the next node. The last node stores the pointer value –1, which indicates
it is the last node.
The global variable FirstEmpty stores the index of the first element in the empty list. This is the
first node in the empty linked list when it is initialised, which is index 0.
The global variable FirstNode stores the index of the first element in the linked list. There is no
data in the linked list when it is initialised, so FirstNode is initialised to –1.
This diagram shows the content of the initialised array.
FirstEmpty = 0
FirstNode = –1
Index
Data
Pointer
0
-1
1
1
-1
2
2
-1
3
3
-1
4
4
-1
5
19
-1
-1
(a) Write program code for the main program to declare and initialise LinkedList, FirstNode
and FirstEmpty.
Save your program as Question3_N24.
Copy and paste the program code into part 3(a) in the evidence document.
[2]
(b) The procedure InsertData() takes five positive integers as input from the user and inserts
these into the linked list.
Each data item is inserted at the front of the linked list.
The table shows the steps to follow depending on the state of the linked list:
Linked list state
Steps
insert the data in the index pointed to by FirstEmpty
change the pointer to the index pointed to by FirstNode
not full
change the values of FirstNode and FirstEmpty
full
end the procedure
Any node that is at the end of the linked list has a pointer of –1.
Write program code for InsertData().
Save your program.
Copy and paste the program code into part 3(b) in the evidence document.
[6]
(c) The procedure OutputLinkedList() outputs the data in the linked list in order by following
the pointers from FirstNode.
Write program code for OutputLinkedList().
(i)
Save your program.
Copy and paste the program code into part 3(c)(i) in the evidence document.
[2]
Amend the main program to call InsertData() and then OutputLinkedList().
(ii)
Save your program.
Copy and paste the program code into part 3(c)(ii) in the evidence document.
[1]
(iii)
Test your program with the test data:
5
1
2
3
8
Take a screenshot of the output.
Save your program.
Copy and paste the screenshot into part 3(c)(iii) in the evidence document.
[1]
(d) The procedure RemoveData() removes a node from the linked list.
The procedure takes the data item to be removed from the linked list as a parameter.
The procedure checks each node in the linked list, starting with the node FirstNode, until it
finds the node to be removed. This node is added to the empty list, and pointers are changed
as appropriate. The procedure only removes the first occurrence of the parameter.
Assume that the data item being removed is in the linked list.
Write program code for RemoveData().
(i)
Save your program.
Copy and paste the program code into part 3(d)(i) in the evidence document.
[5]
(ii)
Amend the main program to:
•
call RemoveData() with the parameter 5
•
output the word "After"
•
call OutputLinkedList().
Save your program.
Copy and paste the program code into part 3(d)(ii) in the evidence document.
[1]
(iii)
Test your program with both sets of given test data:
Test data set 1:
5
6
8
9
5
Test data set 2:
10
7
8
5
6
Take a screenshot of each output.
Save your program.
Copy and paste the screenshot(s) into part 3(d)(iii) in the evidence document.
[1]
Permission to reproduce items where third-party owned material protected by copyr

75
9618/42/O/N/24 · Question 2Oct/Nov 2024Paper 4229 marks
Loading question…
Question text

2
A linear queue data structure is designed using a record structure.
The record structure Queue has the following fields:
•
QueueArray, a 1D array of up to 100 integer values
•
Headpointer, a variable that stores the index of the first data item in QueueArray
•
Tailpointer, a variable that stores the index of the next free location in QueueArray.
(a) Write program code to declare the record structure Queue and its fields.
If your programming language does not support record structures, a class can be declared
instead.
If you are writing in Python, use comments to declare the appropriate data types.
Save your program as Question2_N24.
Copy and paste the program code into part 2(a) in the evidence document.
[3]
(b) The main program creates a new Queue record with the identifier TheQueue. The head
pointer is initialised to –1. The tail pointer is initialised to 0. Each element in the array is
initialised with –1.
Write program code for the main program.
Save your program.
Copy and paste the program code into part 2(b) in the evidence document.
[3]
(c) The pseudocode function Enqueue() inserts an integer value into the queue.
The function is incomplete. There are three incomplete statements.
FUNCTION Enqueue(BYREF AQueue : Queue, BYVAL TheData : INTEGER)
RETURNS INTEGER
IF AQueue.Headpointer = -1 THEN
← …
AQueue.QueueArray[AQueue.Tailpointer]
← 0
AQueue.Headpointer
AQueue.Tailpointer ← AQueue.Tailpointer + 1
RETURN 1
ELSE
IF AQueue.Tailpointer > … THEN
RETURN -1
ELSE
← TheData
AQueue.QueueArray[AQueue.Tailpointer]
← AQueue.Tailpointer …
AQueue.Tailpointer
RETURN 1
ENDIF
ENDIF
ENDFUNCTION
Write program code for Enqueue().
Save your program.
Copy and paste the program code into part 2(c) in the evidence document.
[5]
(d) The function ReturnAllData() accesses TheQueue. It concatenates all the integer
values that have been inserted into the queue’s array, starting from the value stored at
HeadPointer, with a space between each integer value. The string of concatenated values
is returned.
None of the integer values are removed from the queue.
Write program code for ReturnAllData().
Save your program.
Copy and paste the program code into part 2(d) in the evidence document.
[3]
(e) (i)
The main program asks the user to enter 10 integers with values of 0 or greater. It reads
each input repeatedly until a valid number is entered.
All 10 valid inputs are added to the queue, using Enqueue().
If the value returned from Enqueue() is –1, a message is output to state that the queue
is full, otherwise a message is output to state that the item has been added to the queue.
The function ReturnAllData() is called once all 10 integers have been entered and
the return value from the function call is output.
Amend the main program to perform these actions.
Save your program.
Copy and paste the program code into part 2(e)(i) in the evidence document.
[5]
(ii)
Test your program with the following inputs in the order given:
10
9
–1
8
7
6
5
4
3
2
1
Take a screenshot of the output.
Save your program.
Copy and paste the screenshot into part 2(e)(ii) in the evidence document.
[2]
The function Dequeue() accesses TheQueue. The function returns –1 if the queue is empty.
(f)
If the queue is not empty, the function returns the next item in the queue and updates the
relevant pointer(s).
The data is not replaced or deleted from the queue.
Write program code for Dequeue().
Save your program.
Copy and paste the program code into part 2(f) in the evidence document.
[4]
The main program calls Dequeue() twice, and each time it either outputs ‘Queue empty’
(g) (i)
if there is no data in the queue or outputs the return value.
The main program then calls ReturnAllData() a second time.
Amend the main program.
Save your program.
Copy and paste the program code into part 2(g)(i) in the evidence document.
[3]
9618/42/O/N/

Advertisement