6.2: Reading Algorithms
What is an algorithm?
An algorithm is a set of instructions, presented in a logical sequence.
​
In an exam you may be asked to read and understand an algorithm that has been written. To prove your understanding you may be asked to respond by actions such as listing the outputs of the algorithm or identifying an error within it.
Example Algorithm:
Start NewProgram
number is integer
maxvalue is integer
​
input maxvalue
​
for i = 1 to maxvalue
output (i * i)
???????
​
output 'program finished'
​
End NewProgram
Example Questions:
1. List the outputs produced by the algorithm if the 'maxvalue' input is 5.
​
2. State the code that has been replaced by '???????' and what the code's purpose is.
Example Answers:
1.
Outputs:
1
4
9
16
25
program finished
2.
Missing Code: next i
Purpose: Moves the loop to the next iteration.
Previous Exam Questions to try
2014 Exam Algorithm:
set counter = 0
output "Before the loop"
​
repeat
set counter = counter + 1
output "Count is" counter
until counter = 3
​
output "Loop has ended"
2015 Exam Algorithm:
Total is integer
Mean is real
i is integer
​
startmainprog
​
set Total = 0
for i = 1 to 5
set Total = Total + i
output "Total is", Total
next i
​
set Mean = Total / 5
output "Mean is", Mean
​
endmainprog
2014 Exam Question:
Write down all the outputs produced by the algorithm.
2016 Exam Algorithm 1:
M is integer
P is integer
i is integer
​
startmainprog
​
input M
​
for i = 1 to 4
set P = i * M
output P
endfor
​
endmainprog
2015 Exam Question:
2016 Exam Question 1:
Write down all the outputs produced by the algorithm when the value of M input is 3.
Write down all the outputs in the correct order produced by the algorithm.
2016 Exam Algorithm 2:
Total is ? {stores the total of the numbers input}
Mean is ? {stores the mean of the numbers input}
Count is ? {stores the loop control value}
​
startmainprog
​
set Total = 0 {initialise variables}
set Count = 0
​
repeat
set Count = Count + 1
set Total = Total + Count
until Count = 20
​
output "The total is ", Total
set Mean = Total / 20
output "mean is ", Mean
​
endmainprog
2017 Exam Algorithm 1:
HighNum is integer
Total is integer
i is integer
​
startmainprog
​
input HighNum
​
set Total = 0
​
if HighNum < 1 then
output "Number not valid"
else
for i = 1 to HighNum
set Total = Total + i
output Total
endfor
endif
​
endmainprog
2016 Exam Question 2:
State, giving a reason for each, the most suitable data type for the variables below:
a. Mean
b. Count
2017 Exam Question 1:
Write down all the outputs in the correct order produced by the algorithm for the input below:
a. Input is 0
b. Input is 4
2017 Exam Algorithm 2:
IF (X > 5) AND (Y < 7) THEN
OUTPUT true
ELSE
OUTPUT false
ENDIF
2017 Exam Question 2:
Write down the output for the following values of X and Y:
a. X = 2 and Y = 4
b. X = 5 and Y = 4
c. X = 6 and Y = 6
2019 Exam Algorithm:
Start addProc
number is integer
a is integer
total is integer
​
if i = 1 to 5
output "Please enter next number"
input number
a = a + number
next i
​
total = a
output "The total = ", total
​
End addProc
2019 Exam Question:
1. Identify the error in this algorithm and suggest a suitable change to this code to address the error.
​
2. Suggest a suitable change to address the inefficient use of memory in this algorithm.
6.2 - Reading Algorithms:
​
Answer each of the previous exam questions on this page.