top of page

## 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.

Basic Commands

Annotation

//Comments are written using two slashes

Assignment

name = "Harold"

age = 49

â€‹

Constants and Global Variables

constant tax = 15

â€‹

Input / Output

print("Transaction Complete")

â€‹

Casting

str(29)

int("102")

float(30)

bool("False")

â€‹

Random Number

number = random(1,100)

Selection

Selection (if - then - else)

if firstname == "Steven" thenâ€‹

print("Hello" + firstname)

elif firstname == "Steve" then

else

print("Who are you?")

end if

â€‹

Selection (case select)

switch day:

case “Sat”:

print(“It is Saturday”)

case “Sun”:

print(“It is Sunday”)

default:

print(“It is a Weekday”)

endswitch

Iteration

Iteration (for loop)

for i = 1 to 10 step 1

input item

next i

â€‹

Iteration (while loop)

while firstname != "Steven"

firstname = input("Try again:")

endwhile

â€‹

Iteration (do while loop)

do

firstname = input("Guess name:")

until firstname == "Steven"

String Handling

Length of a String

word = "dictionary"

print(word.length)                 outputs 10

â€‹

Substrings

word = "dinosaurs"

print(word.substring(2,3))   outputs nos

print(word.left(3))                  outputs din

print(word.right(4))               outputs aurs

â€‹

Concatenation

name = "Penelope"

surname = "Sunflower"

print(name + surname)

â€‹

String Cases

phrase = "The Cat Sat On The Mat"

print(phrase.lower)

print(phrase.upper)

â€‹

ASCII Conversion

ASC("C")            returns 67

CHR(100)          returns "d"

File Handling

file1 = open("Customers.txt")

while NOT file1.endOfFile()

endwhile

file1.close()

â€‹

File Handling - Writing to a (New) File

newFile("paint.txt")

file2 = open("paint.txt")

paint = input("Enter a paint colour:")

file.writeLine(paint)

file2.close()

Arrays

Declare Array

array names[3]

â€‹

array names = "Ella", "Sam", "Ali"

â€‹

Declare 2D Array

array grid[4,5]

â€‹

Assign Values

names[2] = "Samantha"

grid[1,3] = "X"

More Programming Keywords

Connecting strings together using the + symbol is called concatenation.

â€‹

Extracting certain parts of a string (e.g. using .substring()) is called slicing.

â€‹

An if statement within an if statement or a loop within a loop is called nesting.

## Flowcharts

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:

The terminator symbol is also known as a terminal.

## Algorithm Examples

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.

Pseudocode

//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

endwhile

â€‹

if itemsentered >= 20 then

print("You are on your way to saving money.")

elif itemsentered => 30 then

print("You're a real money saver.")

else

print("Look for better deals next time.")

endif

Flowchart

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.

Example Algorithm:

procedure NewProgram()

â€‹

maxvalue = input()

â€‹

for i = 1 to maxvalue

output (i * i)

???????

â€‹

print("program finished")

â€‹

endprocedure

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.

1.

Outputs:

1

4

9

16

25

program finished

2.

Missing Code: next i

Purpose: Moves the loop to the next iteration.

## Structure Diagrams

Structure diagrams display the organisation (structure) of a problem in a visual format, showing the subsections to a problem and how they link to other subsections.

The noughts and crosses structure diagram below has subsections in light yellow. Each subsection could be coded by a different person.

Structure diagrams are different to flowcharts (those show how data is input, processed and output within a program or system).

â€‹

You may be asked in an exam to draw or fill in a simple structure diagram.

## Trace Tables

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

next i

â€‹

print("complete")

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.

## Questo's Questions

1.2 - Designing Algorithms:

â€‹

1. What is the definition of an algorithm? Name two ways an algorithm can be designed. [3]

â€‹

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

â€‹

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

â€‹

4. What is the purpose of a structure diagram? [2]

â€‹

5. Create a trace table for the NewProgram() algorithm in the Reading Algorithms section on this page. [7]

bottom of page