Data types, arrays and files – A Level Computer Science (old syllabus, to 2021) (9608)
118 questions · page 6 of 8
Question text
A function, ProcessMarks(), is required to analyse test marks for a class of students.
7
•
There are 20 students in the class.
•
A mark is between 0 and 100.
•
The marks for the class are stored in an array, Mark, which has 20 elements.
•
The array is passed to the function as a parameter.
•
The function will output a message stating the average and highest marks. For example:
"The average mark is 34 and the highest mark is 76"
•
The function returns the subscript of the highest mark.
Write program code to implement the ProcessMarks() function.
Visual Basic and Pascal: You should include the declaration statements for variables.
Python: You should show a comment statement for each variable used with its data type.
Programming language …
Program code
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [7]
Appendix
Built-in functions (pseudocode)
Each function returns an error if the function call is not properly formed.
MID(ThisString : STRING, x : INTEGER, y : INTEGER
Question text
1
(a) A farm has a number of greenhouses used to grow vegetables. Each greenhouse has a
different identification number. A program is needed to store temperature information for each
greenhouse throughout the day.
Give a suitable identifier name for each of the data items.
Description of data item
Suitable identifier name
The temperature inside the greenhouse
The temperature outside the greenhouse
The greenhouse identification number
The time of the temperature measurements
[4]
(b) (i)
Program variables have values as follows:
Variable
Value
Mark
60
Subject
"Computer Science"
Grade
'B'
CourseCompleted
TRUE
AverageMark
49.5
Evaluate each expression in the following table.
If an expression is invalid, write ERROR.
For the built-in functions list, refer to the Appendix on page 16.
Expression
Evaluates to
"Fas" & MID(Subject, 6, 3)
LEFT(Mark, 1)
10 + ASC(Grade)
MOD(AverageMark * 2, 3)
CourseCompleted AND (Mark >= 60)
[5]
(ii)
Programming languages support different data types.
Give an appropriate data type for each of these variables from part (b)(i).
Variable
Data type
Mark
Subject
Grade
CourseCompleted
AverageMark
[5]
Question text
3
A chocolate factory produces bars of chocolate. A computer program controls the process.
The weight of each bar is stored in an array, BarWeight. The array contains 100 elements,
representing the weights of 100 bars that make up one shipping box.
A procedure, CheckWeight(), is required to:
1.
examine each array element and count how many times the weight has exceeded MaxWeight
2.
compare the count obtained with a limit value, Threshold. Call procedure ServiceCheck()
if the count exceeds the Threshold
3.
output a message if the count does not exceed the Threshold. For example:
"ShippingBox OK – maximum weight exceeded 3 times."
Draw a program flowchart on the next page to represent the algorithm for the CheckWeight()
procedure.
Assume that:
•
the array contains 100 valid weight values and the first element is BarWeight[1]
•
MaxWeight, Threshold and BarWeight are global variables.
Variable declarations are not required in program flowcharts.
[10]
Question 4 begins on the next page.
Question text
5
A golf club holds information about its members. When a member completes a round of golf, their
score is stored along with their membership number and the date of the round.
A program is to be written to store and process the score information. The information to be stored
is formed into a string as follows:
<MembershipNumber><Date><Score>
(a) The program designer considers storing the strings in either a 1D array, RoundScore or
as a separate variable for each round, for example, RoundScore01, RoundScore02,
RoundScore03 and so on.
Describe two advantages of storing the strings in a 1D array rather than as separate variables.
Advantage 1 …
…
Advantage 2 …
…
[2]
(b) A procedure, AddNewScores() is being developed. The procedure will be coded using
an Integrated Development Environment (IDE).
Name two features provided by an IDE that assist in the initial detection of errors in the
procedure.
Feature 1 …
…
Feature 2 …
…
[2]
(c) The program needs a function, GetNumber(), to return a valid membership number. A valid
membership number is a four-digit numeric string between “1111” and “9999”.
The structured English representing the algorithm for this function is as follows:
1.
Prompt and input a membership number.
2.
Validate the membership number.
3.
Repeat from step 1 if the membership number is invalid.
4.
Return the valid membership number as a string.
An example of the function call in pseudocode is:
GetNumber()
MembershipNumber
Write program code for the GetNumber() function.
Visual Basic and Pascal: You should include the declaration statements for variables.
Python: You should show a comment statement for each variable used with its data type.
Programming language …
Program code
…
…
…
…
…
…
…
…
Question text
6
(a) Individual elements in a 1D array are referenced using an integer value that is used as the
subscript to the array.
Give the technical terms for the minimum and maximum values the subscript may take.
Minimum value …
Maximum value …
[2]
(b) A 2D array, Picture, contains data representing a bitmap image. Each element of the array
represents one pixel of the image. The image is grey-scale encoded where the value of each
pixel ranges from 0 (representing black) to 255 (representing white) with intermediate values
representing different levels of grey.
A graphics program needs a procedure, Flip(), to flip (reflect) the image. An example of an
image before and after the function is:
Before flip
After flip
The values contained in the 2D array before the flip are as follows:
Data values
80
80
255
80
80
255
80
80
80
80
255
80
80
255
80
80
255
80
120
120
120
120
255
80
255
80
255
255
255
255
80
80
255
80
120
120
120
120
80
80
In pseudocode, the array is declared as follows:
DECLARE Picture : ARRAY[1:5, 1:8] OF INTEGER
Write pseudocode to implement the Flip() procedure.
Assume that Picture is a global variable.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
1
(a) A program controls the heating system of an energy-efficient house.
Give a suitable identifier name for each of the data items.
Description of data item
Suitable identifier name
The temperature inside the house
The temperature outside the house
The wind speed
Whether it was raining or not
[4]
(b) (i)
Program variables have values as follows:
Variable
Value
Quality
'D'
DayNumber
20
MyName
"Stephen"
QualityConfirmed
TRUE
Factor
6.5
Evaluate each expression in the following table.
If an expression is invalid, write ERROR.
For the built-in functions list, refer to the Appendix on page 15.
Expression
Evaluates to
MID(MyName, 4, 4) & "ol"
QualityConfirmed AND (Factor >= 6.5)
20 + ASC(Quality)
QualityConfirmed + 3
MOD(Factor * 2, 9)
[5]
(ii)
Programming languages support different data types.
Give an appropriate data type for each of these variables from part (b)(i).
Variable
Data type
QualityConfirmed
DayNumber
Factor
Quality
MyName
[5]
Question text
4
A program controls a chemical process in a factory.
The temperature is monitored as part of the control process. The temperature is measured at
fixed time intervals and the value is stored in an array, PTemp. The array contains 100 elements,
representing 100 temperature values. The first element is PTemp[1].
The program will check whether the temperature is outside the acceptable range more than 20
times. This task is performed by a function, IsTempOK().
The algorithm for the function IsTempOK() is expressed in structured English as follows:
1
Examine each array element and count the number of times that a temperature is less than
MinTemp or more than MaxTemp.
2
If the count in step 1 exceeds 20, return FALSE, otherwise return TRUE.
Draw a program flowchart, on the next page, to represent the algorithm for the function
IsTempOK().
Assume:
•
the array contains 100 valid temperature values
•
PTemp, MinTemp and MaxTemp are global variables.
Note that variable declarations are not required in program flowcharts.
[9]
Question text
5
A golf club holds information about its members. When a member completes a round of golf, their
score is stored along with their membership number and the date of the round.
A program is to be written to store and process the score information.
The information to be stored is as follows:
•
MembershipNumber is a four-digit numeric string.
•
Date is a six-digit numeric string in the format DDMMYY
•
Score is a two-digit numeric string in the range “50” to “99”.
This information is stored in a single string in the format:
<MembershipNumber><Date><Score>
(a) (i)
The program designer considers storing the strings in an array.
State how many dimensions the array would require.
… [1]
(ii)
The designer has decided to store the strings in a file.
Give a reason for this choice.
…
… [1]
(b) The program will be developed using an Integrated Development Environment (IDE). One
feature provided by an IDE is known as prettyprint.
Name two presentation features provided by prettyprint.
Feature 1 …
…
Feature 2 …
…
[2]
(c) The strings are stored in a text file, ScoreDetails.txt.
The program needs a function, GetAverageScore().
The structured English representing the algorithm for this function is as follows:
1
Receive a membership number as its argument.
2
Search the ScoreDetails.txt file for all scores for that member.
3
Calculate the average score.
4
Return the average score.
You can assume that there will be at least one score relating to each member.
Write program code for the GetAverageScore() function.
Visual Basic and Pascal: You should include the declaration statements for variables.
Python: You should show a comment statement for each variable used with its data type.
Programming language …
Program Code
…
…
…
…
…
…
…
…
…
Question text
6
(a) Individual elements in a 1D array are referenced using an integer variable.
For example, using the variable n, an individual element would be referenced in pseudocode
as:
StudentGrade[n]
Give the correct technical term for the variable n.
… [1]
(b) A 2D array, Picture, contains data representing a bitmap image. Each element of the array
represents one pixel of the image. The image is grey-scale encoded where the value of each
pixel ranges from 0 (representing black) to 255 (representing white) with intermediate values
representing different levels of grey.
The following is an example of an image and the corresponding data values for the Picture
array.
Bitmap image
Values
240 10
10
10
10
10
10 240
80
80 240 80
80 240 80
80
80
80 240 80
80 240 80
80
80
80 150 150 150 150 80
80
80
80 240 240 240 240 80
80
80
80 150 150 150 150 80
80
240 240 150 150 150 150 240 240
240 240 150 150 150 150 240 240
In pseudocode, the array is declared as follows:
DECLARE Picture : ARRAY[1:8, 1:8] OF INTEGER
A function, Clip(), is required that will:
•
take an integer parameter, MaxVal, to represent the maximum allowed value for each
pixel
•
limit each pixel value to the maximum allowed value
•
return TRUE if any of the pixel values have been changed, otherwise return FALSE
An example use of the function is:
Clip(234)
IsClipped
This will limit the maximum pixel value within the array to 234. Pixels with a value not greater
than 234 will remain unchanged.
Write pseudocode to implement the Clip() function.
Assume that Picture is a global variable.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
6
An Abstract Data Type (ADT) is used to create a linked list. The linked list is created as an array of
records. The records are of type ListNode.
An example of a record of ListNode is shown in the following table.
Data Field
Value
Player
"Alvaro"
Pointer
1
(a) (i)
Use pseudocode to write a definition for the record type, ListNode.
…
…
…
…
… [3]
(ii)
An array, Scorers, will hold 10 nodes of type ListNode. Use pseudocode to write an
array declaration for this array. The lower bound subscript is 0.
… [2]
(b) The linked list stores ListNode records in alphabetical order of player. The last node in the
linked list always has a Pointer value of –1. The position of the first node in the linked list is
held in the variable ListHead.
After some processing, the array and variables are in the state as follows:
Scorers
ListHead
Player
Pointer
0
0
"Alvaro"
1
1
"Antoine"
3
2
"Dimitri"
7
3 "Cristiano"
2
4
"Gareth"
5
5
"Graziano"
6
6
"Olivier"
8
7
"Erik"
4
8
"Yaya"
9
9
"Zoto"
-1
A recursive function traverses the linked list to search for a player.
An example of calling the function, using pseudocode, is:
SearchList("Gareth", ListHead)
Position
Complete the following pseudocode to implement the function SearchList().
The function will return a value of 99 when a player is not found.
FUNCTION SearchList(Find : STRING, Position : INTEGER) RETURNS INTEGER
IF Scorer[Position].Player = …
THEN
RETURN …
ELSE
IF Scorer[Position].Pointer <> –1
THEN
SearchList(Find, … )
Position
RETURN …
ELSE
RETURN …
ENDIF
ENDIF
ENDFUNCTION
[5]
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 International
Examinations Copyright Acknowledgements Booklet. This is produced for each series of examinations and is freely available to download at www.cie.org.uk after
the live examination series.
Cambridge International Examinations is part of the Cambridge Assessment Group. Cambridge Assessment is the brand name of University of Cambridge Local
Question text
3
The arrays PollData[1:10] and CardData[1:10] store data.
PollData
12
85
52
57
25
11
33
59
56
91
CardData
11
12
25
33
52
56
57
59
91
85
An insertion sort sorts these data.
(a) State why it will take less time to complete an insertion sort on CardData than on PollData.
…
… [1]
(b) The following pseudocode algorithm performs an insertion sort on the CardData array.
Complete the following pseudocode algorithm.
01
ArraySize ← 10
FOR Pointer ← 2 TO …
02
03
ValueToInsert ← CardData[Pointer]
HolePosition ← …
04
WHILE (HolePosition > 1 AND ( … > … ))
05
CardData[HolePosition] ← CardData[ … ]
06
HolePosition ← …
07
08
ENDWHILE
CardData[HolePosition] ← …
09
10
ENDFOR
[7]
(c) (i)
A binary search algorithm is used to find a specific value in an array.
Explain why an array needs to be sorted before a binary search algorithm can be used.
…
…
…
…
…
…
…
… [2]
(ii)
The current contents of CardData are shown.
11
12
25
33
52
56
57
59
85
91
Explain how a binary search will find the value 25 in CardData.
…
…
…
…
…
…
…
…
Question text
6
An Abstract Data Type (ADT) is used to create an unordered binary tree. The binary tree is created
as an array of nodes. Each node consists of a data value and two pointers.
A record type, Node, is declared using pseudocode.
TYPE Node
DECLARE DataValue : STRING
DECLARE LeftPointer : INTEGER
DECLARE RightPointer : INTEGER
ENDTYPE
The following statement declares an array BinaryTree.
DECLARE BinaryTree : ARRAY[0:14] OF Node
A variable, NextNode, points to the next free node.
The following diagram shows a possible node.
ArrayIndex
DataValue
LeftPointer
RightPointer
0
"Red"
1
2
The commands in the following table create and add nodes to the binary tree.
Command
Comment
CreateTree(NodeData)
Sets NextNode to 0.
Writes NodeData into DataValue at the position NextNode
Updates NextNode using NextNode = NextNode + 1
AttachLeft(NodeData, ParentNode)
Writes NodeData into DataValue of NextNode
Sets the LeftPointer of node ParentNode to NextNode
Updates NextNode using NextNode = NextNode + 1
AttachRight(NodeData, ParentNode)
Writes NodeData into DataValue of NextNode
Sets the RightPointer of node ParentNode to NextNode
Updates NextNode using NextNode = NextNode + 1
(a) The following commands are executed.
CreateTree("Red")
AttachLeft("Blue", 0)
AttachRight("Green", 0)
The following diagram shows the current state of the binary tree.
0
"Red"
1
2
1
"Blue"
2 "Green"
…
…
…
…
…
…
…
…
…
…
…
…
Write on the diagram to show the state of the binary tree after the following commands have
been executed.
AttachRight("Black", 2)
AttachLeft("Brown", 2)
AttachLeft("Peach", 3)
AttachLeft("Yellow", 1)
AttachRight("Purple", 1)
AttachLeft("White", 6)
AttachLeft("Pink", 7)
AttachLeft("Grey", 9)
AttachRight("Orange", 9)
[5]
(b) A new command has been added to initialise the pointers of the binary tree to − 1 to indicate
they are not in use.
A leaf is a node of the binary tree which has no children. In the case of this binary tree, a node
with a LeftPointer of − 1 and a RightPointer of − 1 is a leaf.
Write a recursive function, in program code, to traverse the binary tree and output the value
of DataValue for each leaf node.
Programming language …
Program code
…
…
…
…
…
…
…
…
…
…
…
Question text
1
(a) (i)
Procedural high-level languages usually support different data types.
Give an appropriate data type for each data value in the following table.
Data value
Data type
27
"27"
"27.3"
TRUE
27/3/2015
27.3
[6]
(ii)
State an appropriate data structure to store the individual test scores for a class of
students.
… [1]
(iii)
Describe how characters are represented using the ASCII character set.
…
…
…
… [2]
(b) Functions and procedures are subroutines.
Explain why you should use subroutines when designing a program solution.
…
…
…
… [2]
(c) The following pseudocode is an example of nested IF statements.
IF MyVar = 1
THEN
CALL Proc1()
ELSE
IF MyVar = 2
THEN
CALL Proc2()
ELSE
IF MyVar = 3
THEN
CALL Proc3()
ELSE
OUTPUT "Error"
ENDIF
ENDIF
ENDIF
Use pseudocode to write a CASE statement with the same functionality.
…
…
…
…
…
…
…
… [4]
(d) Program coding is a transferable skill.
You are given program code written in a high-level language that you have not studied.
State two different features of the code that you should be able to recognise.
1 …
…
2 …
…
Question text
3
(a) A multi-user computer system stores information about users. It uses a 1D array,
UserNameArray, of type STRING. There are 100 elements in the array.
The format of the string in each element of the array is as follows:
<UserID><UserName>
•
UserID is a six-character string of numerals.
•
UserName is a variable-length string.
Write pseudocode for a procedure, BubbleSort, to perform an efficient bubble sort on
UserNameArray. The array is to be sorted in ascending order of UserID.
You should assume that UserNameArray has been declared as a global variable.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
4
Numeric formatting converts a numeric value to a string in order to present it in a specific way.
In a generic high-level language, formatting is implemented using a mask system. In this system,
each character of the mask corresponds to one character of the formatted string.
Mask characters have the following meaning:
Mask character
Meaning
Character must be a digit or a space
#
Character must be a digit
0
Any other mask characters are taken as literal values and are included in the formatted string.
(a) Using the mask "###00.00", complete the following table. Use □ to represent a space. The
first value has been done for you.
Value
Formatted string
1327.5
"□1327.50"
1234
7.456
[2]
(b) For each row in the following table, define the mask required to produce the formatted output
from the given value. □ represents a space.
Value
Required output
Mask
1234.00
"1,234.00"
3445.66
"£3,445.66"
10345.56
"$□ □10,345"
[3]