COMPUTER SCIENCE 9618

 

PSEUDO-CODE - ARRAYS AND 2-D ARRAYS -Answers

9618/21/M/J/25/Q4(a)  < 1-D ARRAY >

 


9618/21/M/J/25/Q4(b)

(b) Complete the trace table by dry running the algorithm.

        The first row has been completed.

 

MARK SCHEME

 

 

 

9618/21/M/J/25/Q6(b)  < 2-D ARRAY >

Q6) A program monitors the speed of vehicles as they move around a large building site.

 

Each vehicle contains a sensor which reads an integer value that represents the speed of the vehicle. The value is expected to be in the range 0 to 60 inclusive.

 

The sensors cannot read values less than 0.

 

A program module has been written to validate the values read by the sensors.

 

(b) Each sensor used in the system has a unique sensor ID number in the range 1 to 50.

 

The program that is used to monitor the speed of each vehicle reads each sensor value every second and stores this value along with the sensor’s unique ID into a global 2D array Reading.

 

The global array Reading has been declared as follows:

 

DECLARE Reading : ARRAY[1:2000, 1:2] OF INTEGER

 

The array contains 4000 elements organised as 2000 rows and 2 columns.

 

Column 1 contains the sensor value, and column 2 contains the sensor ID.

 

When 2000 sensor readings have been taken, the array is full and the system stops taking any more sensor readings until the array has been processed.

 

A procedure Sort() is needed to sort the array into ascending order of sensor value using an efficient bubble sort algorithm.

 

(b) Write efficient pseudocode for the procedure Sort()     [8]

 

Steps:

 

  

 

 

MARK SCHEME

 

 

 

 

 

9618/22/M/J/25/Q1(d,i,ii,iii)  < 2-D ARRAY >

(d) A global array Product is used as part of the pseudocode design being developed to control the production line. Product is used to store the number of rejected items each day.

 

The following pseudocode statement is used to assign a value to an element of the array:

 

 

The lower and upper bound values are shown in the table:

 

 

(iii) Give the pseudocode declaration for the array Product.     [2]

MARK SCHEME

 

 

9618/22/M/J/25/Q4  < 1-D ARRAY >

Q4) An algorithm will:

 

• input 100 integer values, one value at a time

 

• store the first value input into the first location of the array Number

 

• store the next input value in the next unused location of the array Number

 

• output the contents of Number array in the opposite sequence to that in which the values were input.

 

Complete the program flowchart to represent the algorithm.      [6]

Variable declarations are not required.

MARK SCHEME:

 

 

 

9618/22/M/J/25/Q5(b)  < 1-D ARRAY >

(b) At the end of each sequence of pictures, the time is saved as a string in the format where:

 

• HH represents the hours using two digits

 

• MM represents the minutes using two digits

 

• SS represents the seconds using two digits.

 

For example:

 

• "081230" is stored to represent the time 8:12:30, in the morning

 

• "152235" is stored to represent the time 15:22:35, in the afternoon.

 

Each string is stored on a new line in the text file TimeTaken.txt

 

An algorithm is required to process the content of the text file TimeTaken.txt once it has been transferred to the computer.

 

For each hour when pictures are taken, output a suitable message showing the hour and the total number of sequences of pictures taken within that hour.

 

Example outputs:

 

 

Write pseudocode for this algorithm.

Assume the text file TimeTaken.txt contains at least one line.        [8]

 

 

 

 

9618/22/M/J/25/Q7(a)   < 2-D ARRAY >

Q7) A program is being developed to implement a customer loyalty scheme for a coffee shop.

 

Each customer has a unique customer ID starting at 10001 with this value increasing by one each time a new customer joins the loyalty scheme.

 

For example, the third customer who joins the loyalty scheme is given the customer ID 10003.

 

The loyalty scheme is limited to 1000 customers.

 

A customer is awarded a loyalty point every time they buy a coffee.

 

The programmer has decided to use a global 2D array Loyalty of type INTEGER. The array Loyalty is made up of 1000 rows and 2 columns. Each row relates to one customer; column 1 contains the unique customer ID and column 2 contains the number of customer loyalty points.

 

Rows in the array Loyalty that are not currently being used have the value of Column 1 set to 99999.

 

The array is sorted in ascending order by customer ID.

 

The programmer has defined a program module:

 

 

(a) Write efficient pseudocode for module FindCustomer()        [8]

-------------------------------------------------------------------------------------------

 

--------------------------------------------------------------------------------------------

 

 

OVERALL MARK SCHEME:

9618/22/M/J/25/Q7(b)   < 2-D ARRAY >

(b) A customer can claim a free coffee for every 11 loyalty points.

 

The programmer has defined a second program module:

 

 

Write efficient pseudocode for module PointsReport()

 

Assume the array contains the data for at least one customer.   [7]

 

 

 

 

9618/21/M/J/24/Q4  < 1-D ARRAY>

Q4) A global 1D array Data contains 100 elements of type integer.

 

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]

 

 

9618/21/M/J/24/Q5  < 1-D ARRAY>

Q5) A global 1D array of strings contains three elements which are assigned values as shown:

 

 

Procedure Process() manipulates the values in the array.

 

The procedure is written in pseudocode as follows:

(a) Complete the trace table by dry running the procedure when it is called as follows: [6]

 

MARK SCHEME:

 

 

 

 

 

9618/21/M/J/24/Q7(b.ii)  < 1-D ARRAY>

(ii) Write the pseudocode module headers for Sub-A and Sub-B.

 

 

9618/22/M/J/24/Q3(a.ii)  < 1-D ARRAY>

Q3) 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.

 

 

(ii) A 1D array Item of 2000 elements will store the data for all items.

Write pseudocode to declare the Item array.    [2]

 

 

9618/22/M/J/24/Q2(b)  < 1-D ARRAY>

(b) State three benefits of using an array of records to store the data for all items.  [3]

 

 

9618/23/M/J/24/Q3(a)  < 1-D ARRAY>

Q3) A record structure is declared to hold data relating to components being produced in a factory:

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:

Two global variables contain the minimum and maximum acceptable weight for each component.

 

The values represent an inclusive range and are declared as:

 

(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]

 

 

9618/23/M/J/24/Q2(a)  < 1-D ARRAY>

(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(). [5]

Q8) 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:

 

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:

 

 

 

 

 

9618/23/M/J/24/Q2(a)  < 2-D ARRAY>

 

(b) A new module is required:

As a reminder, the previous example of part of the array is repeated below:

 

 

Write pseudocode for module FindModules().    [8]

Assume that the array contains enough rows for the number of modules in each project.

 

 

 

 

 

9618/21/O/N/24/Q6(b)  < 1-D ARRAY>

Q6) 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):

 

  Note: Leap years are not considered

(b) Complete the pseudocode for the function GetDate().

 

Date functions from the insert should be used in your solution.

 

 

 

 

9618/23/M/J/24/Q8(a)  < 1-D ARRAY>

Q8) 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:

 

 

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:

 

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:

(a) Write pseudocode for module CheckMark().    [6]

LOOP:

 

 

 

9618/23/M/J/24/Q8(a)  < 2-D ARRAY>

(b) A second module is defined:

Write pseudocode for module CheckAll().

 

CheckMark() must be used to check each individual mark.    [8]

 

 

 

 

 

9618/22/O/N/24/Q5(a)  < 1-D ARRAY>

 

Q5) A program contains a global 1D array Data with 100 elements of type INTEGER. The program contains a function Process() expressed in pseudocode as follows:

 

(a) Run-time errors can be generated in different ways. For example, a run-time error will be generated if a function is called with invalid parameters.

 

The pseudocode contains three statements that could generate a run-time error.

 

Write the three statements and explain how each could generate a run-time error.  [3]

 

 

 

9618/22/O/N/24/Q6(a)  < 1-D ARRAY>

Q6) 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:

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:

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().    [7]

 

Assume that both arrays are global.

 

 

 

9618/22/O/N/24/Q8(a)  < 1-D ARRAY>

Q8) 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:

 

 

The programmer has defined a record type to define each character. The record type definition is shown in pseudocode as follows:

 

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:

(a) Write pseudocode for module Assign().   [7]

 

 

 

 

9618/22/O/N/24/Q8(b)  < 1-D ARRAY>

(b) A new module will store the contents of the Character array in a text file.

 

The module is defined as follows:

Complete the pseudocode for module Save().      [7]

 

 

 

 

9618/23/O/N/24/Q4(a)  < 1-D ARRAY>

Q4) 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:

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.           [4]

 

 

9618/23/O/N/24/Q4(b.ii)  < 1-D ARRAY>

(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.         [6]

 

 

ALTERNATIVE – Example using selection Version 1

 

ALTERNATIVE – Example using selection Version 2

 

FOR ALTERNATIVE EXAMPLE 1 AND 2:

9618/23/O/N/24/Q8(a)  < 1-D ARRAY>

Q8) 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:

The programmer has defined a record type to define each character. The record type definition is shown in pseudocode as follows:

 

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:

(a) Complete the pseudocode for module Count().          [7]

 

9618/23/O/N/24/Q8(a)  < 1-D ARRAY>

 

(b) The Character array data has been saved in the text file SaveFile.txt

 

Each line of the file contains one element of the array (one record).

 

New modules are defined:

 

As a reminder, the record structure is repeated here:

Write pseudocode for module Restore().

 

You must use the module Extract()               [7]

 

 

 

9618/21/O/N/23/Q4  < 1-D ARRAY>

Q4) 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]

 

 

 

9618/21/O/N/23/Q4  < 2-D ARRAY>

(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.            [3]

 

 

 

 

 

9618/22/O/N/23/Q5)  < ADT’s>

Q5) A global 1D array of integers contains four elements, which are assigned values as shown:

A procedure Process() manipulates the values in the array.

 

The procedure is written in pseudocode:

 

 

Complete the trace table on the opposite page by dry running the procedure when it is called as follows:         [6]

 

MARK SCHEME:

 

 

 

9618/22/O/N/23/Q8(a)  < 2-D ARRAY>

Q8) 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:

 

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:

(a) Write pseudocode for module GetPort().

 

Assume DestinationID contains a valid three-digit string.         [7]

 

 

 

 

 

9618/23/O/N/23/Q2(a)  < 1-D ARRAY>

Q2) 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.      [5]

               

 

9618/23/O/N/23/Q4(b)  < 1-D ARRAY>

(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:

Write a simplified version of the conditional clause    [1]

 

 

 

9618/23/O/N/23/Q5  < 1-D ARRAY>

Q5) A global 1D array of integers contains four elements, which are assigned values as shown:

A procedure Process() manipulates the values in the array.

 

The procedure is written in pseudocode as follows:

 

 

Complete the trace table on the opposite page by dry running the procedure when it is called as follows:

 

 

 

 

9618/21/M/J/22/Q8(a)  < 2-D ARRAY>

Q8) A program allows a user to save passwords used to login to websites. A stored password is inserted automatically when the user logs into the corresponding website.

 

A student is developing a program to generate a password. The password will be of a fixed format, consisting of three groups of four alphanumeric characters. The groups are separated by the hyphen character '-'.

 

An example of a password is: "FxAf-3haV-Tq49"

 

A global 2D array Secret of type STRING stores the passwords together with the website domain name where they are used. Secret contains 1000 elements organised as 500 rows by 2 columns.

 

Unused elements contain the empty string (""). These may occur anywhere in the array.

 

An example of a part of the array is:

Note:

 

• For security, passwords are stored in an encrypted form, shown as "••••••••••" in the example.

 

• The passwords cannot be used without being decrypted.

 

• Assume that the encrypted form of a password will not be an empty string.

 

The programmer has started to define program modules as follows:

For reference, relevant ASCII values are as follows:

 

(b) A new module is defined as follows:

Write pseudocode for module FindPassword().

Assume that modules Encrypt() and Decrypt() have already been written.         [7]

 

 

9618/22/M/J/22/Q6  < 1-D ARRAY>

Q6) A string represents a series of whole numbers, separated by commas.

 

For example: "12,13,451,22"

 

Assume that:

 

• the comma character ',' is used as a separator

 

• the string contains only the characters '0' to '9' and the comma character ','

 

A procedure Parse will:

 

• take the string as a parameter

 

• extract each number in turn

 

• calculate the total value and average value of all the numbers

 

• output the total and average values with a suitable message.

 

Write pseudocode for the procedure.           [7]

MARK SCHEME:

 

<ON THE NEXT PAGE>

 

               

 

 

9618/22/M/J/22/Q7(a)  < 1-D ARRAY>

 

Q7) A programming language has string functions equivalent to those given in the insert.

 

The language includes a LEFT() and a RIGHT() function, but it does not have a MID() function.

 

(a) Write pseudocode for an algorithm to implement your own version of the MID() function which will operate in the same way as that shown in the insert.     [4]

 

Do not use the MID() function given in the insert, but you may use any of the other functions.

 

Assume that the values passed to the function will be correct.

 

 

 

9618/22/M/J/22/Q8(a)  < 1-D ARRAY>

Q8) A program allows a user to save passwords used to log in to websites. A stored password is then inserted automatically when the user logs in to the corresponding website.

 

A global 2D array Secret of type STRING stores the passwords together with the website domain name where they are used. Secret contains 1000 elements organised as 500 rows by 2 columns.

 

Unused elements contain the empty string (""). These may occur anywhere in the array.

 

An example of a part of the array is:

Note:

 

• For security, the passwords are stored in an encrypted form, shown as "•••••••••"  in the example.

 

• The passwords cannot be used without being decrypted.

 

• You may assume that the encrypted form of a password will NOT be an empty string

 

The programmer has started to define program modules as follows:

Note: in a case-sensitive comparison, 'a' is not the same as 'A'.

(a) Write pseudocode for the module Exists().       [5]

 

 

 

 

9618/22/M/J/22/Q8(b)  < 2-D ARRAY>

(b) A new module SearchDuplicates() will:

 

• search for the first password that occurs more than once in the array and output a message each time a duplicate is found.

 

For example, if the same password was used for the three websites ThisWebsite.com, website27.net and websiteZ99.org, then the following messages will be output:

 

"Password for ThisWebsite.com also used for website27.net"

 

"Password for ThisWebsite.com also used for websiteZ99.org"

 

• end once all messages have been output.

 

The module will output a message if no duplicates are found.

 

For example:

 

"No duplicate passwords found"

 

Write efficient pseudocode for the module SearchDuplicates(). Encrypt() and Decrypt() functions have been written.

 

Note: It is necessary to decrypt each password before checking its value.          [8]

 

 

 

9618/23/M/J/22/Q5(a.i)  < 1-D ARRAY>

Q5) A program will store attendance data about each employee of a company. The data will be held in a record structure of type Employee. The fields that will be needed are as shown:

(a) (i) Write pseudocode to declare the record structure for type Employee.   [4]

MARK SCHEME:

9618/23/M/J/22/Q5(a.ii)  < 1-D ARRAY>

(ii) A 1D array Staff containing 500 elements will be used to store the employee records. Write pseudocode to declare the Staff array.              [2]

 

 

9618/23/M/J/22/Q5(c)  < 1-D ARRAY>

(c) A procedure Absentees() will output the EmployeeNumber and the Name of all employees who have an Attendance value of 90.00 or less.

 

Write pseudocode for the procedure Absentees().

 

Assume that the Staff array is global.          [4]

 

 

9618/22/O/N/22/Q4)  < 1-D ARRAY >

Q4) The program flowchart represents a simple algorithm.

 

(a) Write the equivalent pseudocode for the algorithm represented by the flowchart.

 

 

 

Alternative solution using FOR loop:

 

 

 

9618/22/O/N/22/Q7(a)  < 1-D ARRAY >

Q7) A teacher is designing a program to perform simple syntax checks on programs written by students.

 

Two global 1D arrays are used to store the syntax error data. Both arrays contain 500 elements.

 

• Array ErrCode contains integer values that represent an error number in the range 1 to 800.

 

• Array ErrText contains string values that represent an error description.

 

The following diagram shows an example of the arrays.

 

 

Note:

 

• There may be less than 500 error numbers so corresponding elements in both arrays may be unused. Unused elements in ErrCode have the value 999. The value of unused elements in ErrText is undefined.

 

• Values in the ErrCode array are stored in ascending order but not all values may be present, for example, there may be no error code 31.

 

The teacher has defined two program modules as follows:

 

(a) Write efficient pseudocode for module OutputError().          [6]

 

 

9618/22/O/N/22/Q7(b)  < 1-D ARRAY >

 

(b) Write an efficient bubble sort algorithm in pseudocode for module SortArrays().

 

MARK SCHEME:

 

 

 

9618/23/O/N/22/Q7(a)   < 1-D ARRAY >

 

Q7) A teacher is designing a program to perform simple syntax checks on programs written by students.

 

Two global 1D arrays are used to store the syntax error data. Both arrays contain 500 elements.

 

• Array ErrCode contains integer values that represent an error number in the range 1 to 800.

 

• Array ErrText contains string values that represent an error description.

 

The following diagram shows an example of the arrays.

 

 

Note:

 

• There are less than 500 error codes so corresponding elements in both arrays may be unused. Unused elements in ErrCode have the value 999. These will occur at the end of the array. The value of unused elements in ErrText is undefined.

 

• Values in the ErrCode array are stored in ascending order but not all values may be present. For example, there may be no error code 31.

 

• Some error numbers are undefined. In these instances, the ErrCode array will contain a valid error number but the corresponding ErrText element will contain an empty string.

 

The teacher has defined one program module as follows:

 

 

(a) Write pseudocode for module OutputRange(). Assume that the two numbers input represent a valid error number range    [8]

 

 

 

 

9618/23/O/N/22/Q7(b.i)   < 1-D ARRAY >

(b) (i) Two additional modules are defined:

Write pseudocode for the module AddError(). Assume that the error code is not already in the ErrCode array  [6]

 

Mark SCHEME:

 

< ON THE NEXT PAGE >

 

 

 

9618/21/M/J/21/Q5   < 2-D ARRAY >

Q5) A global 2D array Result of type INTEGER is used to store a list of exam candidate numbers together with their marks. The array contains 2000 elements, organised as 1000 rows and 2 columns.

 

Column 1 contains the candidate number and column 2 contains the mark for the corresponding candidate. All elements contain valid exam result data.

 

A procedure Sort() is needed to sort Result into ascending order of mark using an efficient bubble sort algorithm.

 

Write pseudocode for the procedure Sort().       [8]

 

 

9618/22/M/J/21/Q5(a.i)   < 1-D ARRAY >

Q5a) A student is learning about arrays.

 

She wants to write a program to:

 

• declare a 1D array RNum of 100 elements of type INTEGER

 

• assign each element a random value in the range 1 to 200 inclusive

 

• count and output how many numbers generated were between 66 and 173 inclusive.

 

(i) Write pseudocode to represent the algorithm.           [6]

 

 

 

9618/22/M/J/21/Q6   < 1-D ARRAY >

Q6) A procedure CountVowels() will:

 

• be called with a string containing alphanumeric characters as its parameter

 

• count and output the number of occurrences of each vowel (a, e, i, o, u) in the string

 

• count and output the number of occurrences of the other alphabetic characters (as a single total). The string may contain both upper and lower case characters.

 

Each count value will be stored in a unique element of a global 1D array CharCount of type INTEGER. The array will contain six elements.

 

Write pseudocode for the procedure CountVowels().          [8]

 

 

 

9618/22/M/J/21/Q8(a)   < 1-D ARRAY >

Q8) A program is needed to take a string containing a full name and to produce a new string of initials.

 

Some words in the full name will be ignored. For example, “the”, “and”, “of”, “for” and “to” may all be ignored.

 

Each letter of the new string must be upper case. For example:

The programmer has decided to use the following global variables:

 

• a ten element 1D array IgnoreList of type STRING to store the ignored words

 

• a string FNString to store the full name string.

 

Assume that:

 

• each alphabetic character in the full name string may be either upper or lower case

 

• the full name string contains at least one word.

 

The programmer has started to define program modules as follows:

(a) Write pseudocode for the module IgnoreWord().         [5]

Mark SCHEME:

 

 

 

9618/22/M/J/21/Q8(b)   < 1-D ARRAY >

 

(b) Write pseudocode for the module GetInitials().     [8]

 

Mark SCHEME:

 

< ON THE NEXT PAGE >

 

 

 

 

9618/21/O/N/21/Q5(c)  < 1-D ARRAY >

(c) A function LogEvents() will:

 

• take a Student ID as a parameter

 

• for each element in the array that matches the Student ID parameter:

 

◦ add the value of the array element to the existing text file LogFile

 

◦ assign an empty string to the array element

 

• count the number of lines added to the file

 

• return this count.

 

Write pseudocode for the function LogEvents().             [7]

9618/21/O/N/21/Q6(a)  < 1-D ARRAY >

Q6) A mobile phone has a touchscreen. The screen is represented by a grid, divided into 800 rows and 1280 columns.

 

The grid is represented by a 2D array Screen of type INTEGER. An array element will be set to 0 unless the user touches that part of the screen.

 

Many array elements are set to 1 by a single touch of a finger or a stylus.

 

The following diagram shows a simplified touchscreen. The dark line represents a touch to the screen. All grid elements that are wholly or partly inside the outline will be set to 1.

These elements are shaded. The element shaded in black represents the centre point.

A program is needed to find the coordinates (the row and column) of the centre point. The centre point on the diagram above is row 6, column 11.

 

Assume:

 

• the user may only touch one area at a time

 

• screen rotation does not affect the touchscreen.

 

The programmer has started to define program modules as follows:

(a) Write pseudocode to implement the module SetRow().           [5]

 

 

9618/21/O/N/21/Q6(b)  < 1-D ARRAY >

(b) The module description of SearchInRow() is provided here for reference.

Write pseudocode to implement the module SearchInRow().              [8]

 

 

 

 

9618/22/O/N/21/Q3(b)   < 1-D ARRAY >

(b) A procedure GetIDs() will:

 

• prompt and input the number of a club

 

• output the StudentID of all the students who are members of that club

 

• output a count of all students in the given club.

 

Write pseudocode for the procedure GetIDs  [7]

 

 

 

9618/22/O/N/21/Q6(a)   < 2-D ARRAY >

 

Q6) A mobile phone has a touchscreen. The screen is represented by a grid, divided into 800 rows and 1280 columns.

 

The grid is represented by a 2D array Screen of type INTEGER. An array element will be set to 0 unless the user touches that part of the screen.

 

Many array elements will set to 1 by a single touch of a finger or a stylus.

 

The following diagram shows a simplified touchscreen. The dark line represents a touch on the screen. All grid elements that are wholly or partly inside the outline will be set to 1.

 

These elements are shaded. The element shaded in black represents the centre point of the touch.

A program is needed to find the coordinates (the row and column) of the centre point. The centre point on the diagram shown is row 6, column 11.

 

 Assume:

 

• the user may only touch one area at a time

 

• screen rotation does not affect the touchscreen.

 

The programmer has decided to use global values CentreRow and CentreCol as coordinate values for the centre point.

 

The programmer has started to define program modules as follows:

(a) Write efficient pseudocode for the module FirstRowSet().          [7]