Algorithm QP
Algorithm QP
12
5 A company creates two new websites, Site X and Site Y, for selling bicycles.
These programs will use data about daily sales made from Site X (using variable SalesX) and
Site Y (using variable SalesY).
28 01/07/2015 14 8
...............................................................................................................................................[2]
5 A firm employs workers who assemble amplifiers. Each member of staff works an agreed number
of hours each day.
The firm records the number of completed amplifiers made by each employee each day.
Daily hours
worked Production data
Worker 1 5 Worker 1 Worker 2 Worker 3
Worker 2 10 Day 1 10 20 9
Worker 3 10 Day 2 11 16 11
Day 3 10 24 13
Day 4 14 20 17
1 ........................................................................................................................................
...........................................................................................................................................
2 ........................................................................................................................................
.......................................................................................................................................[2]
.......................................................................................................................................[1]
...........................................................................................................................................
.......................................................................................................................................[2]
Write the program code. Do not attempt to include any validation checks.
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.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
.............................................................................................................................................[10]
8 In this question you will need to use the given pseudocode built-in function:
A program reads a string entered by the user. The string represents the addition or subtraction
of two fractions. Each part of the fraction within the string is always a single digit only and the
top digit is always less than the bottom digit.
Op
é
N4Char
N3Char
N2Char
N1Char
The identifier table shows the variables to be used to store the characters in the string as
shown in the diagram.
(b) A program is to be written which accepts a string and then calculates a numeric value from
this string. The input string and the calculated value are then to be sent to a remote computer
over a communications link.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................[6]
(c) Explain the purpose of sending the value of StringTotal to the remote computer, in
addition to MyString.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................[2]
Num1 ← 5
A ← ASC('F') + Num1 + ASC('Z') (i) A ..................................................... [1]
B ← CHR(89) & CHR(69) & CHR(83) (ii) B ..................................................... [1]
C ← CHARACTERCOUNT(B & "PLEASE") (iii) C ..................................................... [1]
D ← ASC(ONECHAR("CURRY SAUCE", 7)) (iv) D ..................................................... [1]
The function:
• has identifier RemoveSpaces
• has a single parameter
• will include the declaration for any local variables used by the function
// main program
INPUT MyString
ChangedString ← RemoveSpaces(.........................................................................................)
OUTPUT ChangedString
// function definition
.................................................................................................................................................
.................................................................................................................................................
.................................................................................................................................................
.................................................................................................................................................
j ← CHARACTERCOUNT(InputString)
FOR i ← 1 TO j
NextChar ← ONECHAR(InputString, i)
IF NextChar <> " "
THEN
// the & character joins together two strings
NewString ← NewString & NextChar
ENDIF
ENDFOR
.................................................................................................................................................
ENDFUNCTION
[7]
3 A string encryption function is needed. The encryption uses a simple character-substitution method.
In this method, a new character substitutes for each character in the original string. This will create
the encrypted string.
The substitution uses the 7-bit ASCII value for each character. This value is used as an index for a
1D array, Lookup, which contains the substitute characters.
Lookup contains an entry for each of the ASCII characters. It may be assumed that the original
string and the substitute characters are all printable.
For example:
The programmer writes a function, EncryptString, to return the encrypted string. This function
will receive two parameters, the original, PlainText string and the 1D array.
(a) The first attempt at writing the pseudocode for this function is shown below.
For the built-in functions list, refer to the Appendix on page 18.
DECLARE n : INTEGER
.............................//concatenate to OutString
ENDFOR
.............................................................
ENDFUNCTION
[10]
© UCLES 2016 9608/21/M/J/16 [Turn over
8
(b) Additional code needs to be written to allow the user to change some of the characters in the
array Lookup.
The first version of the algorithm is represented by the flowchart on the following page.
...........................................................................................................................................
...........................................................................................................................................
.......................................................................................................................................[2]
In addition to the Lookup array, assume that the following variables have been declared:
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
.......................................................................................................................................[6]
START
Input StartPos
Input NumToChange
n 0
Loop
Input NewChar
Increment n
No
n = NumToChange – 1?
Yes
Output message
stating number of
elements changed
STOP
The encryption uses the 7-bit ASCII value for each character. This value is used as an index for a
1D array, Lookup, which contains the substitute characters.
Lookup contains an entry for each of the ASCII characters.
This function, Decrypt, will accept two parameters, a single character, CipherChar, and the 1D
array, Lookup.
(a) The first attempt at writing the pseudocode for this function is shown below.
For the built-in functions list, refer to the Appendix on the last page.
Index 1 // ........................................................................................................
Found FALSE
//search for CipherChar in Lookup:
WHILE .....................................................................................................................................
//compare CipherChar with this array element:
IF .....................................................................................................................................
THEN
..........................................................................//Set the flag
ELSE
Index .....................................................//Move to next array element
ENDIF
ENDWHILE
//dropped out of loop so must have found CipherChar:
RETURN ...................................................................................................................................
ENDFUNCTION
[11]
START
Index StartIndex
Loop
End of NO
loop?
YES
STOP
For example, for the input of 65 and 3, the output will be:
In addition to the Lookup array, assume that the following variables have been declared:
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
[6]
(a) At College, before joining the company, Ahmed used two items of software for programming:
• a text editor
• a compiler
Describe how he could have developed programs using these software tools.
Include in the description the terms ‘object code’ and ‘source code’.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................[3]
(b) Ahmed now uses an Integrated Development Environment (IDE) for programming.
(i) State one feature an IDE provides to help with the identification of syntax errors.
...........................................................................................................................................
.......................................................................................................................................[1]
(ii) State one feature an IDE provides to carry out white box testing.
...........................................................................................................................................
.......................................................................................................................................[1]
(c) The company maintains a file of product data. Ahmed is to write a program to add a new
product and search for a product based on the structure diagram shown:
Product
processing
By By By
product code description price
• product code
• product description
• product retail price
The text file PRODUCTS stores each data item on a separate line, as shown below:
File PRODUCTS
0198
Plums(10kg)
11.50
0202
Onions(20kg)
10.00
0376
Mango chutney(1kg)
02.99
0014
Mango(10kg)
12.75
(i) The first operation of the program is to read all the product data held in file PRODUCTS
and write them into the three 1D arrays.
OPEN ................................................................................................................................
i 1
WHILE ..............................................................................................................................
.......................................................................................................................................
.......................................................................................................................................
ENDWHILE
CLOSE "PRODUCTS"
(ii) State one benefit and one drawback of this file design.
Benefit ...............................................................................................................................
...........................................................................................................................................
Drawback ..........................................................................................................................
.......................................................................................................................................[2]
(d) To code the ‘Search by product code’ procedure, Ahmed draws a structure chart showing the
different stages.
You can assume that before the procedure is run, all the product data is read from file
PRODUCTS and then stored in three 1D arrays as described in part (c)(i).
Lookup by
product code
......................
......................
......................
.................
................ ......................
......................
[4]
(e) A first attempt was made at writing the ‘Search for product code’ module.
Ahmed designs this as a function ProductCodeSearch.
• if the product code is found, it returns the index position of the 1D array PCode being
searched
• if the product code is not found, the function returns -1
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.
......................................................................................................................................
......................................................................................................................................
......................................................................................................................................
......................................................................................................................................
......................................................................................................................................
......................................................................................................................................
......................................................................................................................................
......................................................................................................................................
......................................................................................................................................
......................................................................................................................................
......................................................................................................................................
......................................................................................................................................
......................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................[6]
Write the equivalent pseudocode using a pre-condition loop, for this part of the amended
flowchart.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
.............................................................................................................................................. [3]
2 You will need to refer to the list of pseudocode string-handling functions in the Appendix.
(a) Give the value of the variables x, y and z for the following sequence of statements.
y ............................ [1]
A computer program is to simulate the reading and processing of a string of characters from an
input device.
13*156*9*86*1463*18*#
(i) A statement which declares a global variable used to store a single character. ........... [1]
(iii) A statement which indicates the start of a ‘pre-condition’ loop. ........... [1]
(c) Copy the condition which is used to control the inner loop.
.............................................................................................................................................. [1]
© UCLES 2016 9608/22/O/N/16
7
(d) (i) Complete the trace table below for the given pseudocode as far as line 27.
Numbers
i j NextChar NextNumberString
1 2 3
1 1 '2'
""
"2"
2 '3' "23"
3 '*' 23
[5]
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...................................................................................................................................... [2]
© UCLES 2016 9608/22/O/N/16 [Turn over
QUESTION 5. 4
2 One of the security features of a multi-user computer system is a user login process. The user
must complete this successfully before they can access the resources of the system.
As part of the login process the user enters their user ID followed by a password. The system then
compares the password entered with the password held in a file.
(a) The steps involved in the login process are described as follows:
The description above is not detailed enough to allow a program to be written. The validation
checks must be expressed as a more detailed algorithm.
Give the name of the process of increasing the level of detail of the algorithm.
...............................................................................................................................................[1]
(b) An identifier table is created as the algorithm is developed. A section of the table is shown.
Complete the table.
........................................................................
PasswordInput ...................
........................................................................
........................................................................
UserIDFound ...................
........................................................................
........................................................................
PasswordValid ...................
........................................................................
[5]
(c) The validation checks described in part (a) are to be used as the basis for program code.
You should:
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................[8]
2 A multi-user computer system maintains a text file containing the ID and preferred name for each
user.
User IDs are unique. Preferred names may be repeated.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
.............................................................................................................................................. [2]
(b) An initial identifier table is created as part of the stepwise refinement. A section of the table is
shown. Complete this table.
...........................................................................
FileUserID
...........................................................................
...........................................................................
FilePreferredName
...........................................................................
...........................................................................
IDFoundFlag
...........................................................................
[5]
(c) The file may not contain an entry for every user. If the user ID is not found, then the name
“Guest” should be used as the preferred name.
Use structured English to write a more detailed algorithm for the welcome message process
described in part (a).
Assume that the file, UserNames.txt, is organised as a simple text file. The user ID and
preferred name are held together in unencrypted form, one entry per line.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
.............................................................................................................................................. [8]
INPUT StartNumber
INPUT EndNumber
INPUT Divisor
NumberFound 0
For the built-in functions list, refer to the Appendix on page 14.
[3]
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................[3]
[10]
INPUT SearchValue
FoundFlag FALSE
Index 1
IF FoundFlag = FALSE
THEN
OUTPUT "Not found"
ENDIF
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
.............................................................................................................................................. [2]
[9]
2 Shop customers have a discount card with a unique card number. Customers collect points after
they have bought items. The more points they have, the bigger the discount. If they shop on a
Wednesday, their discount is increased by 20%.
The function GetDiscountRate() takes a card number as a parameter and returns the discount
rate for a customer based on the number of points they have collected. A flowchart for the function
is shown.
START
DRate 0
Points
GetPoints(CardNum)
Is YES
Points > 199 ? DRate 0.2
NO
Is YES
Points > 99 ? DRate 0.1
NO
NO
RETURN DRate
END
Your solution should follow the flowchart for the function as closely as possible. Variable
declarations should be included.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................[8]
Name and describe one other type of error that the function could contain.
Name .................................................................................................................................
Description ........................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
[2]
Name and describe a strategy that can be used to test GetDiscountRate() before
the GetPoints() function has been written.
Name .................................................................................................................................
Description ........................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
[2]
(c) There are different ways to minimise the risk of errors when writing programs, such as the
use of constants and library routines.
(i) Identify two values that could be replaced by constants in the function
GetDiscountRate().
...........................................................................................................................................
.......................................................................................................................................[1]
(ii) Write pseudocode to declare one of the constants you have given in part (c)(i).
.......................................................................................................................................[2]
(iii) Explain how the use of constants helps to minimise programming errors.
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
.......................................................................................................................................[2]
(iv) Give a reason why the use of library routines helps to minimise the risk of errors when
writing a program.
...........................................................................................................................................
.......................................................................................................................................[1]
(v) Constants and library routines help to minimise the risk of errors.
Name another way that you can minimise the risk of errors when writing a program.
Explain how this helps.
Name .................................................................................................................................
Explanation .......................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
[2]
You are asked to work on a program written in a language you are not familiar with.
Explain how transferable skills would help you work on the program.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................[2]
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................[2]
(c) A program needs to search through 1000 elements of an unsorted array to find a given value.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................[4]
© UCLES 2018 9608/22/O/N/18
9
3 (a) A student is developing an algorithm to search through a 1D array of 100 elements. Each
element of the array, Result, contains a REAL value.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
............................................................................................................................................. [7]
(b) The student decides to change the algorithm and implement it as a procedure, ScanArray(),
which will be called with three parameters.
ScanArray() will modify the first two parameters so that the new values are available to the
calling program or module.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
............................................................................................................................................. [4]
(b) There is an error in the algorithm. When called as shown in part (a)(i), the function did not
return the largest value as expected.
(i) Explain why this error occurred when the program called the function.
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
..................................................................................................................................... [2]
(ii) Describe how the algorithm could be amended to correct the error.
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
..................................................................................................................................... [2]
5 A student is learning about text files. She wants to write a program to count the number of lines in
a file.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
............................................................................................................................................. [3]
(b) A procedure, CountLines(), is being written to count the number of lines in a text file. The
procedure will:
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
............................................................................................................................................. [6]
5 Nigel is learning about string handling. He wants to write code to count the number of words in a
given string. A word is defined as a sequence of alphabetic characters that is separated by one or
more space characters.
NumWords 0
ENDPROCEDURE
For the built-in functions list, refer to the Appendix on page 18.
His first attempt is incorrect. He will use white-box testing to help him to identify the problem.
...........................................................................................................................................
..................................................................................................................................... [1]
(ii) Dry running the code is often used in white-box testing. In this method, the programmer
records the values of variables as they change.
Identify what the programmer would normally use to record the changes.
..................................................................................................................................... [1]
(b) (i) Write a test string containing two words that gives the output:
Number of words : 2
Use the symbol '∇' to represent each space character in your test string.
String .................................................................................................................................
Explanation .......................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
[3]
String 1: "Red∇and∇Yellow"
String 2: "Green∇∇and∇∇Pink∇"
Describe the changes that would need to be made to the algorithm to give the correct
output in each case.
String 1 ..............................................................................................................................
Description ........................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
String 2 ..............................................................................................................................
Description ........................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
[6]
4 A student is developing a program to count how many times each character of the alphabet (A to Z)
occurs in a given string. Upper case and lower case characters will be counted as the same. The
string may contain non-alphabetic characters, which should be ignored.
• check each character in the string to count how many times each alphabetic character occurs
• store the count for each alphabetic character in a 1D array
• output each count together with the corresponding character.
(a) The student has written a structured English description of the algorithm:
The student decides to apply a process to increase the level of detail given in step 4.
State the name of the process and use this process to write step 4 in more detail. Use
structured English for your answer.
Process .....................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
[4]
You should assume the following lines of pseudocode have been written:
Declare any further variables you use. Do not implement the code as a subroutine.
Refer to the Appendix on pages 16–17 for the list of built-in functions and operators.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
............................................................................................................................................. [7]
PROCEDURE FillTank()
Tries 1
Full ReadSensor("F1")
IF NOT Full
THEN
WHILE NOT Full AND Tries < 4
CALL TopUp()
Full ReadSensor("F1")
Tries Tries + 1
ENDWHILE
IF Tries > 3
THEN
OUTPUT "Too many attempts"
ELSE
OUTPUT "Tank now full"
ENDIF
ELSE
OUTPUT "Already full"
ENDIF
ENDPROCEDURE
(a) (i) The pseudocode includes features that make it easier to read and understand.
Feature 1 ...........................................................................................................................
Feature 2 ...........................................................................................................................
Feature 3 ...........................................................................................................................
[3]
(ii) Draw a program flowchart to represent the algorithm implemented in the pseudocode.
Variable declarations are not required in program flowcharts.
[5]
Complete the table by giving a suitable data type for each example value.
43
TRUE
− 273.16
"− 273.16"
[4]
Refer to the Appendix on page 18 for the list of built-in functions and operators.
Expression Evaluates to
MID(NUM_TO_STRING(2019), 3, 1)
INT(NUM_TO_STRING(-273.16))
INT(13/2)
[4]
4 The following pseudocode algorithm checks whether a string is a valid email address.
NumDots 0
NumAts 0
NumOthers 0
ENDFOR
ENDFUNCTION
(a) Describe the validation rules that are implemented by this pseudocode. Refer only to the
contents of the string and not to features of the pseudocode.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
............................................................................................................................................. [3]
(b) (i) Complete the trace table by dry running the function when it is called as follows:
Result Check("[email protected]")
[5]
(ii) State the value returned when function Check is called as shown in part (b)(i).
..................................................................................................................................... [1]
State two different invalid string values that could be used to test the algorithm. Each string
should test a different rule.
Value .........................................................................................................................................
Justification ...............................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
Value .........................................................................................................................................
Justification ...............................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
[4]
5 Abbreviations are often used in place of a full name. Concatenating the first letter of each word in
the name makes an abbreviation.
For example:
Name Abbreviation
United Nations UN
A function, Abbreviate(), will take a string representing the full name and return a string
containing the abbreviated form.