1.2: Designing 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, correcting errors or identifying an error within it.
Programmers create algorithm designs as a method of planning a program before writing any code. This helps them to consider the potential problems of the program and makes it easier to start creating source code.
There are two main methods of defining algorithms are pseudocode and flowcharts. In exams, OCR will display algorithms in their own 'OCR Exam Reference Language'.
OCR Exam Reference Language
The OCR exams require specific questions to be written either in OCR Exam Reference Language (shown below) or a high-level programming language such as Python.
//Comments are written using two slashes
name = "Harold"
age = 49
Constants and Global Variables
constant tax = 15
global name = "Admin"
Input / Output
name = input("Enter your name")
number = random(1,100)
Selection (if - then - else)
if firstname == "Steven" then
print("Hello" + firstname)
elif firstname == "Steve" then
print("Please use full name")
print("Who are you?")
Selection (case select)
print(“It is Saturday”)
print(“It is Sunday”)
print(“It is a Weekday”)
Iteration (for loop)
for i = 1 to 10 step 1
Iteration (while loop)
while firstname != "Steven"
firstname = input("Try again:")
Iteration (do while loop)
firstname = input("Guess name:")
until firstname == "Steven"
Length of a String
word = "dictionary"
print(word.length) outputs 10
word = "dinosaurs"
print(word.substring(2,3)) outputs nos
print(word.left(3)) outputs din
print(word.right(4)) outputs aurs
name = "Penelope"
surname = "Sunflower"
print(name + surname)
phrase = "The Cat Sat On The Mat"
ASC("C") returns 67
CHR(100) returns "d"
File Handling - Reading Lines
file1 = open("Customers.txt")
while NOT file1.endOfFile()
File Handling - Writing to a (New) File
file2 = open("paint.txt")
paint = input("Enter a paint colour:")
array names = "Ella", "Sam", "Ali"
Declare 2D Array
names = "Samantha"
grid[1,3] = "X"
A flowchart can be used to visually represent an algorithm.
It is more likely you will need to be able to interpret a flowchart rather than draw one.
The flowchart symbols are:
Below are two different methods for representing the same algorithm - a program to encourage people to buy items cheaply at a supermarket.
The program allows the price of items in a supermarket to be entered until the total reaches 100. The total price and the number of items entered are tracked as the program loops. Once the total reaches 100 or more, an if statement checks how many items have been entered and a different message is printed if there are 20 or more items, 30 or more items or less than 20 items.
//This is a program to see how many items you can buy in a supermarket
before you spend over £100}
total = 0
itemsentered = 0
while total < 100
itemprice = input("enter the price of the next item")
total = total + itemprice
itemsentered = itemsentered + 1
if itemsentered >= 20 then
print("You are on your way to saving money.")
elif itemsentered => 30 then
print("You're a real money saver.")
print("Look for better deals next time.")
In an exam you may be asked to read an algorithm and prove your understanding, most commonly by listing the outputs.
Start from the first line and follow the program line by line, recording the value of variables as you go.
When you encounter a for loop, repeat the indented code as many times as stated in the range.
maxvalue = input()
for i = 1 to maxvalue
output (i * i)
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.
Missing Code: next i
Purpose: Moves the loop to the next iteration.
Watch on YouTube
Trace tables are used to track the value of variables as a program is run.
They can be used to manually track the values in order to investigate why the program isn't working as intended.
Each row in the trace table represents another iteration. Each column stores the value of a variable as it changes.
See below how the trace table is updated for the simple algorithm on the left.
num1 = 2
num2 = 5
for i = 1 to 3
output (num1 + num2)
num2 = num2 - 1
For most algorithms, not every variable will be updated in each iteration.
Values may not be entered in the order of the trace table either. For example, each iteration outputs num1 + num2 and then decreases the value of num2 by 1.
1.2 - Designing Algorithms:
1. What is the definition of an algorithm? Name two ways an algorithm can be designed. 
2. Using a high-level programming language such as Python, or the OCR Exam Reference Language, write an algorithm that inputs 6 decimal numbers and outputs the total, largest, smallest and average values. 
For example, entering 3.1, 5.3, 2.3, 5.4, 2.9 and 4.4 would output 23.3 (total), 5.4 (largest), 2.3 (smallest) and 3.9 (average).
3. Draw and label the flowchart symbols. 
4. Create a trace table for the NewProgram() algorithm in the Reading Algorithms section on this page.