OCR H446, section 1.2.3 (c)
What is an algorithm?
An algorithm is a sequence of clear steps that solves a problem. It must be unambiguous, have definite inputs and outputs, and finish after a finite number of steps.
Before writing code, programmers design the algorithm using pseudocode or a flowchart, then check it by following it by hand.
OCR pseudocode
OCR's exam papers use this style of pseudocode. You aren't expected to memorise the exact syntax: you may answer in any reasonable style that a competent programmer could understand. But you should recognise this one.
Variables, input and output
x = 3 // assignment uses =
name = "Bob"
global userid = 123 // global variable in the main program
print("hello")
answer = input("Enter your name")
y = int("3") // casting: int, str, float
A variable is created the first time a value is assigned, and takes the type of that value. Comments start with //.
Operators
| Kind | Operators |
|---|---|
| Arithmetic | + - * / ^ (power), MOD (remainder), DIV (whole number quotient) |
| Comparison | == != < <= > >= |
| Logical | AND OR NOT |
Examples: 12 MOD 5 is 2, 17 DIV 5 is 3, 3^4 is 81.
Selection
if entry == "a" then
print("You selected A")
elseif entry == "b" then
print("You selected B")
else
print("Unrecognised")
endif
switch entry:
case "A":
print("You selected A")
case "B":
print("You selected B")
default:
print("Unrecognised")
endswitch
Iteration
for i = 0 to 7 // count-controlled: i is 0, 1, ... 7
print("Hello")
next i
while answer != "computer" // condition-controlled, test at the start
answer = input("Password?")
endwhile
do // condition-controlled, test at the end
answer = input("Password?")
until answer == "computer"
Subroutines
function triple(number)
return number * 3
endfunction
procedure greeting(name)
print("hello " + name)
endprocedure
y = triple(7) // y is 21
greeting("Hamish")
Parameters are passed by value unless stated. procedure foobar(x:byVal, y:byRef) shows which.
Strings, arrays and files
someText = "Computer Science"
print(someText.length) // 16
print(someText.subString(3, 3)) // starts at position 0: "put"
array names[5] // arrays are 0-based
names[0] = "Ahmad"
array board[8,8] // a 2D array
myFile = openRead("sample.txt")
while NOT myFile.endOfFile()
print(myFile.readLine())
endwhile
myFile.close()
myFile = openWrite("out.txt")
myFile.writeLine("Hello World")
myFile.close()
Flowcharts
A flowchart shows an algorithm as a diagram, using standard shapes.
| Shape | Meaning |
|---|---|
| Oval | Start or end |
| Rectangle | A process or instruction |
| Parallelogram | Input or output |
| Diamond | A decision (yes or no) |
| Arrows | The flow of control |
Following an algorithm: trace tables
To follow (trace) an algorithm, act like the computer: go through it step by step, writing down the value of each variable as it changes. A trace table has a column for each variable and for the output.
Algorithm:
total = 0
count = 0
n = int(input("Number"))
while n != 0
total = total + n
count = count + 1
n = int(input("Number"))
endwhile
if count > 0 then
print(total / count)
endif
Trace it with the inputs 4, 6, 0:
| n | total | count | Output |
|---|---|---|---|
| 4 | 4 | 1 | |
| 6 | 10 | 2 | |
| 0 | 10 | 2 | 5 |
The program averages the numbers entered. The algorithm terminates when the user enters 0, and the if count > 0 check stops a division by zero if the first number is 0.
Writing an algorithm
- Understand the problem: what are the inputs, the outputs, and the rules?
- Break it down into steps (see computational thinking in section 2.1).
- Write it in pseudocode or draw a flowchart.
- Test it with sample data by tracing it, including unusual cases.
- Convert to a programming language.
Example: the largest number in an array
largest = numbers[0]
for i = 1 to numbers.length - 1
if numbers[i] > largest then
largest = numbers[i]
endif
next i
print(largest)
Tracing it for [4, 9, 2, 7]:
| i | numbers[i] | numbers[i] > largest? | largest |
|---|---|---|---|
| 4 | |||
| 1 | 9 | yes | 9 |
| 2 | 2 | no | 9 |
| 3 | 7 | no | 9 |
The output is 9.
Worked example
Task: write pseudocode to read 5 numbers from the user and print how many were greater than 10. Then trace it for the inputs 12, 3, 15, 10, 11.
count = 0
for i = 1 to 5
n = int(input("Enter a number"))
if n > 10 then
count = count + 1
endif
next i
print(count)
| i | n | n > 10? | count |
|---|---|---|---|
| 0 | |||
| 1 | 12 | yes | 1 |
| 2 | 3 | no | 1 |
| 3 | 15 | yes | 2 |
| 4 | 10 | no | 2 |
| 5 | 11 | yes | 3 |
The output is 3. Notice that 10 is not greater than 10, so it isn't counted. That is the kind of boundary a careful trace shows up.
Exam tip: in a trace table, add a new row each time a variable changes, and include every variable and the output. Don't skip steps. When writing pseudocode, any clear style is accepted, but be careful with loop limits and with > against >=.
Check yourself
What is an algorithm?
A sequence of clear steps that solves a problem, which is unambiguous and finishes after a finite number of steps.
What is the purpose of a trace table?
To follow an algorithm by hand, recording how the values of variables and the output change at each step, so you can check what it does or find errors.
What is 23 MOD 5, and what is 23 DIV 5?
23 MOD 5 is 3 (the remainder) and 23 DIV 5 is 4 (the whole number quotient).
What is the difference between while and do ... until in OCR pseudocode?
while tests its condition at the start, so the body may never run. do ... until tests at the end, so the body always runs at least once.