Data types, records, arrays and abstract data types – A Level Computer Science (9618)
112 questions · page 5 of 8 · Clear filters
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]
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().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
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]
Question text
3
The diagram represents an Abstract Data Type (ADT).
The operation of this stack may be summarised as follows:
•
The TopOfStack pointer points to the last item added to the stack.
•
The BottomOfStack pointer points to the first item on the stack.
Stack
← TopOfStack
D1
D3
D4
D5
← BottomOfStack
D2
(a) The stack is implemented using two variables and a 1D array of 8 elements as shown.
The variables are used to reference individual elements of the array, in such a way that:
•
the array is filled from the lowest indexed element towards the highest
•
all the elements of the array are available for the stack.
Complete the diagram to represent the state of the stack as shown above.
Array
Data
element
8
7
6
5
Variable
TopOfStack
4
BottomOfStack
3
2
1
[3]
(b) A function Push() will add a value onto the stack by manipulating the array and variables in
part (a).
Before adding a value onto the stack, the algorithm will check that space is available.
If the value is added to the stack, the function will return TRUE, otherwise it will return FALSE.
The algorithm is expressed in five steps.
Complete the steps.
1.
If … then return FALSE
2.
Otherwise … TopOfStack
3.
Use TopOfStack as an … to the array.
4.
Set the element at this … to the … being added.
5.
Return … .
[5]
Question text
4
A global array is declared in pseudocode as follows:
DECLARE Data : ARRAY[1:150] OF STRING
A function TooMany() will:
1.
take two parameters:
•
a string (the search string)
•
an integer (the maximum value)
2.
count the number of strings in the array that exactly match the search string
3.
return TRUE if the count is greater than the maximum value, otherwise will return FALSE
(a) Write pseudocode for the function TooMany().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [6]
(b) The global array is changed to a 2D array, organised as 150 rows by 2 columns. It is declared
in pseudocode as follows:
DECLARE Data : ARRAY[1:150, 1:2] OF STRING
The algorithm for the function in part (a) is changed. Strings will only be counted if both of
the following conditions are true:
•
The current row is an even number.
•
The search string exactly matches the value in either column.
Write pseudocode to check these conditions.
Assume that the row index is contained in variable Row and the search string in variable
Search.
…
…
…
Question text
8
A class of students are developing a program to send data between computers. Many computers
are connected together to form a wired network. Serial ports are used to connect one computer to
another.
Each computer:
•
is assigned a unique three-digit ID
•
has three ports, each identified by an integer value
•
is connected to between one and three other computers.
Messages are sent between computers as a string of characters organised into fields as shown:
<STX><DestinationID><SourceID><Data><ETX>
Field
Field name
Description
number
a single character marking the start of the message
STX
n/a
(ASCII value 02)
DestinationID
1
three numeric characters that identify the destination computer
SourceID
2
three numeric characters that identify the source computer
a variable length string containing the data being sent
Data
3
(Minimum length is 1 character)
a single character marking the end of the message
ETX
n/a
(ASCII value 03)
For example, the following message contains the data "Hello Kevin" being sent from computer
"101" to computer "232":
<STX>"232101Hello Kevin"<ETX>
Each computer will run a copy of the same program. Each program will contain a global variable,
MyID of type string, that contains the unique ID of the computer in which the program is running.
The programmer has defined the first two program modules as follows:
Module
Description
• takes two parameters:
o a string containing a message
Transmit()
(already written)
o an integer containing a port number
• transmits the message using the given port
• takes three parameters:
o a string containing a text file name
o a string containing a Destination ID
SendFile()
o an integer containing a Port number
• transmits the file one line at a time
• transmits a final message with data string "****"
(a) Write pseudocode for module SendFile().
Assume:
•
module Transmit() has already been written and is used to transmit a message
•
the value of MyID may be used as SourceID
•
the file specified contains no blank lines
•
the file specified does not contain the line "****"
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
1
A shop sells car accessories. A customer order is created if an item cannot be supplied from
current stock. A program is being developed to create and manage the customer orders.
(a) The following identifier table shows some of the data that will be stored for each order.
Complete the identifier table by adding meaningful variable names and appropriate data
types.
Example
Explanation
Variable name
Data type
value
"Mr Khan"
The name of the customer
3
The number of items in the order
TRUE
To indicate whether this is a new customer
15.75
The deposit paid by the customer
[4]
(b) Other variables in the program have example values as shown:
Variable
Example value
Total
124.00
DepRate
2.00
Description
"AB12345:Cleaning Brush (small)"
Complete the table by evaluating each expression using the example values.
Expression
Evaluates to
(Total * DepRate) + 1.5
RIGHT(Description, 7)
(LENGTH(Description) - 8) > 16
NUM_TO_STR(INT(DepRate * 10)) & '%'
[4]
(c) The data that needs to be stored for each customer order in part (a) is not all of the same
type.
Describe an effective way of storing this data for many customer orders while the program is
running.
…
…
…
…
…
… [3]
Question text
3
The diagram represents a linked list Abstract Data Type (ADT).
•
Ptr1 is the start pointer. Ptr2 is the free list pointer.
•
Labels D40, D32, D11 and D100 represent the data items of nodes in the list.
•
Labels F1, F2, F3 and F4 represent the data items of nodes in the free list.
•
The symbol Ø represents a null pointer.
Ptr1
D40
D32
D11
D100
Ø
Ptr2
F1
F2
F3
F4
Ø
(a) The linked list is implemented using two variables and two 1D arrays as shown.
The pointer variables and the elements of the Pointer array store the indices (index numbers)
of elements in the Data array.
Complete the diagram to show how the linked list as shown above may be represented using
the variables and arrays.
Variable
Value
Start_Pointer
Free_List_Pointer
5
Index
Data array
Pointer array
1
D32
2
2
3
3
4
D40
5
6
F2
7
7
8
[5]
(b) The original linked list is to be modified. A new node D6 is inserted between nodes D32 and
D11.
Ptr1
D40
D32
D11
D100
Ø
Ptr2
F1
F2
F3
F4
Ø
The algorithm required is expressed in four steps as shown.
Complete the steps.
1.
Assign the data item … to … .
2.
Set the … of this node to point to … .
3.
Set Ptr2 to point to … .
4.
Set pointer of … to point to … .
[4]
Question text
5
A global 1D array of integers contains four elements, which are assigned values as shown:
← 1
Mix[1]
← 3
Mix[2]
← 4
Mix[3]
← 2
Mix[4]
A procedure Process() manipulates the values in the array.
The procedure is written in pseudocode:
PROCEDURE Process(Start : INTEGER)
DECLARE Value, Index, Count : INTEGER
← Start
Index
← 0
Count
REPEAT
←
Mix[Index]
Value
←
Mix[Index] - 1
Mix[Index]
←
Value
Index
←
Count + 1
Count
UNTIL Count = 5
← Count * Index
Mix[4]
ENDPROCEDURE
Complete the trace table on the opposite page by dry running the procedure when it is called as
follows:
CALL Process(3)
Index
Value
Count
Mix[1]
Mix[2]
Mix[3]
Mix[4]
[6]
Question text
8
A class of students are developing a program to send data between computers. Many computers
are connected together to form a wired network. Serial ports are used to connect one computer to
another.
Each computer:
•
is assigned a unique three-digit ID
•
has three ports, each identified by an integer value
•
is connected to between one and three other computers.
Data is sent as individual message strings.
Each string contains the destination ID (the ID of the computer that is to receive the message)
followed by the data:
<DestinationID><Data>
Messages may pass through several computers on the way to their destination.
When a message arrives at a computer, that is not the destination, the program needs to forward
it on to another computer using one of its serial ports.
The port to use is obtained from information that is stored in an array RouteTable.
RouteTable is a global 2D array of integers. It is declared in pseudocode as follows:
DECLARE RouteTable : ARRAY[1:6,1:3] OF INTEGER
The values in the first two columns of RouteTable define a range of ID values.
Column 3 gives the corresponding port number to use when forwarding the message to a computer
with an ID within this range.
For example, the contents of RouteTable could be:
Column 1
Column 2
Column 3
100
199
1
Row 1
200
259
2
Row 2
−1
<undefined>
<undefined>
Row 3
260
399
2
Row 4
400
599
3
Row 5
600
999
1
Row 6
In this example, a message that arrives with a DestinationID of "283" will be forwarded using
port 2.
Row 3 in the example shows an unused row. These may occur anywhere. Unused rows have the
column 1 element set to −1. The value of unused elements in the other two columns is undefined.
The programmer has defined the first program module as follows:
Module
Description
•
takes a DestinationID as a parameter of type string
•
searches for the range corresponding to the DestinationID
GetPort()
in the array
•
returns the port number, or returns −1 if no corresponding
range is found
(a) Write pseudocode for module GetPort().
Assume DestinationID contains a valid three-digit string.
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
2
Data is a 1D array of integers, containing 30 elements. All element values are unique.
(a) An algorithm will output the index of the element with the smallest value.
Draw a program flowchart to represent the algorithm.
START
END
[5]
(b) The 30 data values could have been stored in separate variables rather than in an array.
Explain the benefits of using an array when designing a solution to part (a).
…
… [2]
(c) The requirement changes. Array Data needs to hold 120 elements and each value may
include a decimal place.
Write a pseudocode statement to declare the modified array.
…
… [2]
Question text
3
The diagram represents a queue Abstract Data Type (ADT).
The organisation of this queue may be summarised as follows:
•
The FrontOfQueue pointer points to the next data item to be removed.
•
The EndOfQueue pointer points to the last data item added.
Queue
← FrontOfQueue
D3
D4
D1
D2
← EndOfQueue
D5
The queue is implemented using three variables and a 1D array of eight elements as shown. The
variable NumItems stores the number of items in the queue.
The pointer variables store indices (index numbers) of the array.
(a) Complete the diagram to represent the state of the queue as shown above.
Index
Array
1
2
3
4
Variable
FrontOfQueue
5
EndOfQueue
6
NumItems
7
5
8
[3]
(b) A module AddTo() will add a value to the queue by manipulating the array and variables in
part (a).
The queue implementation is circular. When pointers reach the end of the queue, they will
‘wrap around’ to the beginning.
Before a value can be added to the queue, it is necessary to check the queue is not full.
The algorithm to add a value to the queue is expressed in six steps.
Complete the steps.
1.
If NumItems … then jump to step 6.
2.
Increment … .
3.
If … then set EndOfQueue to … .
4.
Increment … .
5.
Set the … at the index stored in … to the …
being added.
6.
Stop.
[6]
Question text
4
A procedure RandList() will output a sequence of 25 random integers, where each integer is
larger than the previous one.
(a) Write pseudocode for procedure RandList().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [6]
(b) Procedure RandList() is modified so that the random numbers are also written into a
1D array Result.
A new module is written to confirm that the numbers in the array are in ascending order.
This module contains an IF statement that performs a comparison between elements:
IF (Result[x + 1] = Result[x]) OR (Result[x] > Result[x + 1]) THEN
←
FALSE
Sequence
ENDIF
Write a simplified version of the conditional clause.
…
…
… [1]
Question text
5
A global 1D array of integers contains four elements, which are assigned values as shown:
← 4
Mix[1]
← 2
Mix[2]
← 3
Mix[3]
← 5
Mix[4]
A procedure Process() manipulates the values in the array.
The procedure is written in pseudocode as follows:
PROCEDURE Process(Start : INTEGER)
DECLARE Value, Index, Total : INTEGER
← Start
Index
← 0
Total
WHILE Total < 20
←
Mix[Index]
Value
←
Total + Value
Total
IF Index < 4 THEN
←
Mix[Index] + Mix[Index+1]
Mix[Index]
ELSE
←
Mix[Index] + Mix[1]
Mix[Index]
ENDIF
←
(Value MOD 4) + 1
Index
ENDWHILE
← Total * Index
Mix[1]
ENDPROCEDURE
Complete the trace table on the opposite page by dry running the procedure when it is called as
follows:
CALL Process(2)
Index
Value
Total
Mix[1]
Mix[2]
Mix[3]
Mix[4]
[6]
Question text
2
A program contains an algorithm to output a string of a specified length containing identical
characters.
(a) The algorithm is described as follows:
1.
prompt and input a character and store in MyChar
2.
prompt and input an integer and store in MyCount
3.
generate a string consisting of MyChar repeated MyCount times
4.
output the string.
Draw a program flowchart to represent the algorithm.
START
END
[4]
(b) A different part of the program uses the variable StartDate.
Write pseudocode statements to declare StartDate and assign to it the date corresponding
to 15/11/2005.
Declaration …
Assignment …
[3]