7.3

7.3 Explain the Purpose of an Algorithm

Understanding what an algorithm is, how algorithms can be presented, and how to explain the purpose and processes of a given algorithm.

Learning Objectives

By the end of this lesson, you will be able to:

  • Definewhat an algorithm is: a specific sequence of steps that can be followed to complete a task
  • Explainthat a computer program is an implementation of an algorithm, but an algorithm is not a computer program itself
  • Identifythe three main ways an algorithm can be designed: structure diagrams, flowcharts and pseudocode
  • Statethe purpose of a given algorithm clearly and concisely
  • Describethe individual processes involved in an algorithm step by step
  • Interpretpseudocode and flowcharts and explain what they do
  • Applytechniques to understand complex algorithms: looking for comments, considering context, and testing with different inputs

Key Terms

Algorithm

A specific sequence of steps that can be followed to complete a task. A precise set of rules or instructions to solve a specific problem.

Computer program

An implementation of an algorithm. The algorithm itself is not a computer program.

Structure diagram

Shows hierarchical top-down design in a visual form. Each problem is divided into sub-problems, and each sub-problem into further sub-problems.

Flowchart

A visual tool that uses shapes to represent different functions to describe an algorithm. Shows input, output, processes, decisions and repetition.

Pseudocode

A text-based tool that uses short English words and statements to describe an algorithm. More structured than English sentences but very flexible.

Purpose

A clear, concise statement of the problem the algorithm addresses and the expected outcome. Should be specific.

Processes

The individual steps or operations that the algorithm performs to achieve its purpose.

Sequence

Steps carried out one after another in order.

Selection

A decision point where the algorithm follows different paths based on a condition (IF...THEN...ELSE).

Iteration

Repetition of steps in an algorithm (loops such as FOR, WHILE, REPEAT).

Terminator symbol

The oval shape used in a flowchart to show the start or stop of an algorithm.

Input/Output symbol

The parallelogram shape used in a flowchart to show data entering or leaving the algorithm.

Process symbol

The rectangle shape used in a flowchart to show a process or action.

Decision symbol

The diamond shape used in a flowchart to show a yes/no question or condition.

1. What is an Algorithm?

Analgorithmis nothing more than aspecific sequence of stepsthat can be followed to complete a task. It is a precise set of rules or instructions to solve a specific problem or task.

Key idea

Acomputer programis animplementation of an algorithm. An algorithm isnota computer program in itself — it is the logic and steps that the program will follow.

1.1 The Sandwich Example

Making a sandwich is an everyday task that can be broken down into a sequence of steps — an algorithm. Here are the choices you might make:

🍞

1. White or brown bread?

🧈

2. Spread?
Butter or margarine?

🍅

3. Sauce?

🥬

4. Filling
Lettuce? Tomatoes? Bacon?

Each of these decisions is a step in the algorithm. Following the steps in order — with choices at each stage — produces the final sandwich. In the same way, a computer program follows an algorithm to produce a result.

Real-Life Algorithm: Making a Sandwich

  1. Choose white or brown bread
  2. Spread butter or margarine
  3. Add sauce
  4. Add filling (lettuce, tomatoes, bacon)
  5. Put the two slices of bread together
  6. Cut the sandwich (optional)

This is a sequence of steps that can be followed to complete the task of making a sandwich. It is an algorithm.

1.2 Algorithm Building Blocks

Every algorithm can be built from three fundamental building blocks:

Sequence

Steps are carried out one after another, in order.

Selection

A decision is made, and the algorithm follows different paths based on a condition.

Iteration

Steps are repeated (loops) until a condition is met.

Visual: Sequence, Selection and Iteration

This diagram shows how the three building blocks fit together in an algorithm.

StartSEQUENCEProcess / ActionCondition?(Yes / No)SELECTIONNoITERATIONYesStop

How to read it:The algorithm moves from Start, through aprocess(sequence), to adecision(selection). If the answer is "No", the flow loops back to repeat the process (iteration). If "Yes", the algorithm moves to Stop.

Activity 1: Everyday Algorithms

Write a sequence of steps (an algorithm) for each of the following everyday tasks:

  1. Brushing your teeth
  2. Making a cup of tea
  3. Getting ready for school in the morning

For each algorithm, identify whereselection(a decision) occurs.

Example Answers:

1. Brushing your teeth:

  1. Pick up toothbrush
  2. Apply toothpaste
  3. Brush teeth for 2 minutes
  4. Rinse mouth

2. Making a cup of tea:

  1. Boil water
  2. Put tea bag in cup
  3. Pour water into cup
  4. Selection:Do you want milk? If yes, add milk.
  5. Selection:Do you want sugar? If yes, add sugar.
  6. Stir

3. Getting ready for school:

  1. Wake up
  2. Get out of bed
  3. Wash and dress
  4. Eat breakfast
  5. Selection:Have you packed your bag? If not, pack it.
  6. Leave for school

Check Your Understanding: What is an Algorithm?

Answer
  • [1 mark]A specific sequence of steps
  • [1 mark]That can be followed to complete a task / solve a specific problem
Answer
  • [1 mark]A computer program is an implementation of an algorithm
  • [1 mark]The algorithm is the logic/steps; the program is the code that carries them out
  • [1 mark]An algorithm is not a computer program in itself — it can exist without being written in code
Answer
  • [1 mark]Sequence
  • [1 mark]Selection
  • [1 mark]Iteration
Answer

[1 mark]Any suitable everyday task (e.g. making a sandwich, crossing the road, getting ready for school).

[2 marks]A correct sequence of at least three steps in the correct order.

Example:Making a sandwich: (1) choose bread, (2) add spread, (3) add filling, (4) close the sandwich.

Answer
  • [2 marks]Selectionis a decision point where the algorithm chooses between different paths based on a condition (e.g. IF...THEN...ELSE)
  • [2 marks]Iterationis the repetition of steps in a loop (e.g. FOR, WHILE, REPEAT) until a condition is met
Answer
  • [1 mark]So the task is completed correctly and consistently every time
  • [1 mark]If steps are missed or carried out in the wrong order, the algorithm will not produce the correct result

2. Three Ways to Design an Algorithm

There are three main ways to design an algorithm. In the exam you need to be able tocreate, interpret, correct and refinealgorithms using these methods.

Structure diagrams

Hierarchical top-down design in a visual form

Flowcharts

Visual tool using shapes to represent functions

Pseudocode

Text-based tool using short English statements

2.1 Structure Diagrams

Structure diagrams showhierarchical top-down designin a visual form. Each problem is divided into sub-problems and each sub-problem is divided into further sub-problems. At each level the problem is broken down into more detailed tasks that can be implemented using asingle subroutine.

Example: Mobile Phone Structure Diagram

A structure diagram for a mobile application shows how the top-level "Mobile phone" is broken down:

Mobile phone
Voice calls
Text messages
Contact data store
Vibrate
Receive voice
Receive data
Convert to analogue
Send voice

The "Send voice" branch is broken down further into "Capture voice", "Convert to digital" and "Transmit on network" — each of these would be implemented as a single subroutine.

2.2 Flowcharts

Flowcharts are avisual toolthat uses shapes to represent different functions to describe an algorithm. They show the data that isinputandoutput, theprocessesthat take place and anydecisionsorrepetition. Lines are used to show theflow of control.

Terminator
Terminator
Start / Stop (oval)
Input / Output
Input / Output
Data in/out (parallelogram)
Process
Process
Action (rectangle)
Decision
Decision
Yes/No question (diamond)

Example: Age Check for a Casino Site

A casino would like a program that asks users to enter an age. If they are 18 or over they can enter the site; if not, they are given a suitable message.

StartINPUT ageIS age >= 18NoOUTPUT "Sorry, thissite is for users 18and over"YesOUTPUT "Welcome to the site"Stop

Refinement:The casino would like the algorithm refined so that the user also enters their first name and this is used to greet the user when they access the site. This means adding an extraINPUT Namestep and changing the welcome output to include the name.

2.3 Pseudocode

Pseudocode is atext-basedtool that uses short English words/statements to describe an algorithm. Pseudocode ismore structured than writing sentences in Englishbut is very flexible.

Examiner Tips and Tricks

Pseudocode isnot real code— don't use syntax likeprint()orinput()with brackets. Stick to simple statements likeINPUT AgeandOUTPUT "Welcome". Adding actual language syntax can lose you marks.

Example: Age Check Pseudocode

INPUT Age
IF Age >= 18 THEN
OUTPUT "Welcome to the site"
ELSE
OUTPUT "Sorry, this site is for users 18 and over"
ENDIF

Refined Version (also asks for name)

INPUT Name
INPUT Age
IF Age >= 18 THEN
OUTPUT "Welcome to the site", Name
ELSE
OUTPUT "Sorry, this site is for users 18 and over"
ENDIF
Examiner Tips and Tricks

If the question asks you to write an algorithm,default to pseudocode. Use flowcharts only when asked or when visual logic helps. Examiners reward clarity, not decoration.

Same Algorithm, Three Representations

The pseudocode and flowchart above both represent thesame algorithm. Implementing this algorithm as a solution will produce a computer program that asks the user to input two numbers and outputs which of the two is larger.

  • Structure diagram— shows the hierarchy of sub-problems
  • Flowchart— visual, using shapes and arrows
  • Pseudocode— text-based, using simple statements

Activity 2: From Flowchart to Pseudocode

The flowchart below asks the user for two numbers and outputs the larger one.

  1. Write the CIE pseudocode for this algorithm.
  2. Describe what the algorithm does in one sentence.
Solution:
  1. CIE pseudocode:
    INPUT number1
    INPUT number2
    IF number1 > number2 THEN
    OUTPUT "The largest number is ", number1
    ELSE
    OUTPUT "The largest number is ", number2
    ENDIF
  2. Description:The algorithm asks the user to input two numbers and then outputs the larger of the two numbers.

Check Your Understanding: Three Ways to Design an Algorithm

Answer
  • [1 mark]Structure diagrams
  • [1 mark]Flowcharts
  • [1 mark]Pseudocode
Answer
  • [1 mark]A structure diagram shows hierarchical top-down design in a visual form
  • [1 mark]Each problem is divided into sub-problems and each sub-problem into further sub-problems
  • [1 mark]At each level the problem is broken down into more detailed tasks that can be implemented using a single subroutine
Answer
  • [1 mark]Oval (terminator) — start or stop
  • [1 mark]Parallelogram (input/output) — data entering or leaving the algorithm
  • [1 mark]Rectangle (process) — a process or action
  • [1 mark]Diamond (decision) — a yes/no question or condition
Answer
  • [1 mark]Pseudocode is a text-based tool that uses short English words/statements to describe an algorithm
  • [1 mark]It is more structured than writing sentences in English but is very flexible
  • [1 mark]It is not real code — it does not use the syntax of any particular programming language, so it can be translated into any language later
Answer
INPUT Age
IF Age >= 18 THEN
OUTPUT "Welcome to the site"
ELSE
OUTPUT "Sorry, this site is for users 18 and over"
ENDIF

Marking:INPUT statement (1), correct IF condition (1), correct THEN output (1), correct ELSE output (1), ENDIF (1).

Answer
  • [1 mark]Pseudocode is quicker to write and easier to edit than drawing a flowchart
  • [1 mark]Examiners reward clarity — pseudocode uses simple statements that are easy to mark, whereas a messy flowchart can lose marks for unclear symbols or arrows

3. Explaining the Purpose of an Algorithm

In the exam, you will need to be able toexplain the purpose of a specific algorithm. The algorithm might be presented as aflowchartor aspseudocode.

3.1 Stating the Purpose

An algorithm is a step-by-step procedure or formula for solving a problem or completing a task. Algorithms are designed to achieve a specific objective or solve a particular problem efficiently and accurately.

How to state the purpose

When stating the purpose of an algorithm, it is essential to clearly define:

  • What problem it addresses
  • What the expected outcome is

The purpose should bespecific and concise, outlining the problem-solving goal the algorithm aims to achieve.

Example of Stating the Purpose

"The purpose of the algorithm is to sort a list of integers in ascending order."

In this example, the purpose is clear: the algorithm aims to arrange a given list of integers in ascending order, ensuring that the numbers are sorted from smallest to largest.

3.2 Describing the Processes

When describing the processes involved in an algorithm, it is crucial to detail theindividual steps or operationsthat the algorithm performs. This description should provide a clear and precise explanation of how the algorithm achieves its purpose. It often involves:

  • Breaking down the problem into smaller sub-problems
  • Defining the sequence of steps to be executed
  • Specifying any conditions or loops involved

Example: Factorial of a Non-Negative Integer

Consider the problem of finding the factorial of a non-negative integer. The factorial of a number is the product of all positive integers from 1 up to that number.

Algorithm Purpose:"The purpose of the algorithm is to calculate the factorial of a non-negative integer."

Algorithm Processes:

  1. Input:Receive a non-negative integer n as input.
  2. Initialization:Set factorial to 1 (initial factorial value).
  3. Loop:For each integer i from 1 to n, repeat steps 4 and 5.
  4. Multiplication:Multiply factorial by i.
  5. Update Factorial:Update factorial with the result of the multiplication.
  6. Output:Return the calculated factorial as the output.

This description breaks down the algorithm into its individual processes, clearly outlining the steps involved in calculating the factorial of a non-negative integer.

3.3 How Do You Explain an Algorithm?

A well-designed algorithm should be able to be interpreted by a new user, and they should be able to explain what it does.

Algorithms can be written using flowcharts, pseudocode or high-level programming language code such as Python. The purpose of an algorithm is to solve a problem. If a user does not know the goal of the algorithm, then following the algorithm instructions should make its purpose clear.

If the algorithm is complex, additional ways to understand it:
  • Look for comments in the code
  • Consider the context of where the algorithm is being used
  • Test the algorithm with different inputs

Example: What Does This Algorithm Do?

Count ← 1
Number ← 0
Total ← 0
REPEAT
INPUT Number
Total ← Total + Number
Count ← Count + 1
UNTIL Count > 10
OUTPUT Total

Purpose

The purpose of the algorithm is to add ten user-entered numbers together and output the total.

The processes are:

  • Initializing three variables (Count, Number, Total)
  • Inputting a user number
  • Adding to two variables (Total, Count)
  • Repeating nine more times
  • Outputting the final Total value

Activity 3: Explain the Purpose

Look at the following pseudocode algorithm:

Total ← 0
Count ← 0
WHILE Count < 5 DO
INPUT Score
Total ← Total + Score
Count ← Count + 1
ENDWHILE
Average ← Total / 5
OUTPUT Average
  1. State the purpose of the algorithm in one sentence.
  2. List the processes involved in the algorithm.
Solution:
  1. Purpose:The purpose of the algorithm is to input five scores, calculate their average, and output the average.
  2. Processes:
    • Initialising Total and Count to 0
    • Repeating the following five times: inputting a score, adding it to Total, and incrementing Count
    • Calculating the average by dividing Total by 5
    • Outputting the average

Check Your Understanding: Explaining the Purpose

Answer
  • [1 mark]What problem the algorithm addresses
  • [1 mark]What the expected outcome is
Answer
  • [1 mark]So that anyone reading it can understand exactly what the algorithm is for, without confusion
  • [1 mark]A precise statement of purpose makes it easier to check whether the algorithm actually achieves its goal
Answer
  • [2 marks]The purpose of the algorithm is to input ten numbers, add them together, and output the total.
Answer
  • [1 mark]Look for comments in the code
  • [1 mark]Consider the context of where the algorithm is being used
  • [1 mark]Test the algorithm with different inputs
Answer
  • [1 mark]Because the steps in the algorithm show exactly what data is input, what processing happens, and what output is produced
  • [1 mark]By tracing through the steps, a reader can work out what problem the algorithm is solving
Answer
  • [2 marks]The purpose of the algorithm is to ask the user for a password. It gives the user up to three attempts to enter the correct password ("secret"). The loop stops when the correct password is entered or after three attempts.

4. Worked Example: Grade Assignment Algorithm

The pseudocode algorithm shown has been written by a teacher to enter marks for the students in her class and then to apply some simple processing.

Count ← 0
REPEAT
INPUT Score[Count]
IF Score[Count] = 70 THEN
Grade[Count] ← "A"
ELSE IF Score[Count] = 60 THEN
Grade[Count] ← "B"
ELSE IF Score[Count] = 50 THEN
Grade[Count] ← "C"
ELSE IF Score[Count] = 40 THEN
Grade[Count] ← "D"
ELSE IF Score[Count] = 30 THEN
Grade[Count] ← "E"
ELSE
Grade[Count] ← "F"
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
Count ← Count + 1
UNTIL Count = 30

Describe what happens in this algorithm.

Show Mark Scheme

Answer (any 3 of):
  • Inputted marks are stored in the arrayScore[]
  • Marks are then checked against a range of boundaries
  • A matching grade is assigned to each mark that has been input
  • The grade is then stored in the arrayGrade[]
  • At the same index as the inputted mark
  • The algorithm finishes after 30 marks have been input

Understanding the Algorithm

Purpose:To input 30 student marks and assign each mark a letter grade (A–F) based on its value.

Processes:

  • Initialise a counter (Count) to 0
  • Repeat 30 times: input a score and store it in the Score array
  • Compare the score against grade boundaries (70, 60, 50, 40, 30)
  • Store the corresponding grade in the Grade array at the same index
  • Increment the counter

5. Past Paper Practice

Question 1 — Program Development Life Cycle

Four descriptions of stages in the program development life cycle are shown. Draw one line to link each description to its most appropriate program development life cycle stage. Not all stages will be used.

DescriptionStage
develop an algorithm to solve the problem by using structure diagrams, flowcharts or pseudocodeanalysis / coding / design / evaluation / testing
detect and fix the errors in the programanalysis / coding / design / evaluation / testing
identify the problem and its requirementsanalysis / coding / design / evaluation / testing
write and implement the instructions to solve the problemanalysis / coding / design / evaluation / testing

Show Mark Scheme

Answer (4 marks):
  • develop an algorithm... →design
  • detect and fix the errors... →testing
  • identify the problem and its requirements →analysis
  • write and implement the instructions... →coding

Question 2 — Component Parts After Decomposition

Identify three of the component parts after a problem has been decomposed. [3 marks]

Show Mark Scheme

Answer (3 marks):
  • inputs— what is put into the system
  • processes— actions taken to achieve a result
  • outputs— what is taken out of the system
  • storage— what needs to be kept for future use

Question 3 — First Stage of the SDLC

Tick one box to identify the first stage of the program development life cycle. [1 mark]

A Analysis
B Coding
C Design
D Testing

Show Mark Scheme

Answer:

A Analysis— [1 mark]

Analysis comes first because you must understand the problem before you can design a solution.

Question 4 — Ways to Present a Design

Identify three different ways that the design of a solution to a problem can be presented. [3 marks]

Show Mark Scheme

Answer (3 marks):
  • Structure diagram / chart
  • Flowchart
  • Pseudocode

Question 5 — Food Ordering System Structure Diagram

A food ordering system is an example of a computer system that is made up of sub-systems. The food ordering system:

  • allows the user to enter the details of the food they want to order and to pay for the order
  • displays food available as pictures or as a list

Complete the structure diagram for the given parts of the food ordering system. [4 marks]

Food ordering system
User Input
Display options
Display choice
Food order
Payment
Pictures
List

Show Mark Scheme

Mark Scheme (max 4):
  • [1 mark]A suitable hierarchical structure
  • [1 mark]Suitable names for the sub-systems for user input and display options
  • [1 mark]Sub-systems for user inputs (choice of display, food order and payment)
  • [1 mark]Sub-systems for display output types (pictures and list)

Key Takeaways

  • Analgorithmis a specific sequence of steps that can be followed to complete a task.
  • Acomputer programis an implementation of an algorithm — but an algorithm is not a computer program in itself.
  • Every algorithm can be built from three building blocks:sequence,selectionanditeration.
  • There arethree main ways to design an algorithm: structure diagrams, flowcharts and pseudocode.
  • Structure diagramsshow hierarchical top-down design — each problem is divided into sub-problems, and each sub-problem into further sub-problems.
  • Flowchartsuse standard shapes: oval (terminator), parallelogram (input/output), rectangle (process) and diamond (decision).
  • Pseudocodeis a text-based tool using short English statements. It is not real code — don't use language-specific syntax likeprint().
  • Whenstating the purposeof an algorithm, clearly define what problem it addresses and what the expected outcome is.
  • Whendescribing the processesof an algorithm, detail the individual steps, conditions and loops involved.
  • To understand a complex algorithm, look forcomments, consider thecontext, andtest with different inputs.
  • Following the algorithm instructions should make itspurpose clearto a new user.

Question Bank

Answer
  • [1 mark]An algorithm is a specific sequence of steps that can be followed to complete a task
  • [1 mark]It is a precise set of rules or instructions to solve a specific problem
  • [1 mark]A computer program is an implementation of an algorithm
  • [1 mark]An algorithm is not a computer program in itself — it can exist independently of any programming language
Answer
  • [2 marks]Structure diagrams— show hierarchical top-down design in a visual form; each problem is divided into sub-problems and each sub-problem into further sub-problems
  • [2 marks]Flowcharts— a visual tool that uses shapes to represent different functions; shows input, output, processes, decisions and repetition using lines to show the flow of control
  • [2 marks]Pseudocode— a text-based tool that uses short English words/statements to describe an algorithm; more structured than English but very flexible
Answer
  • [1 mark]Oval (terminator) — start or stop
  • [1 mark]Parallelogram (input/output) — data entering or leaving the algorithm
  • [1 mark]Rectangle (process) — a process or action
  • [1 mark]Diamond (decision) — a yes/no question or condition
Answer

Purpose:

  • [1 mark]The purpose of the algorithm is to add ten user-entered numbers together and output the total

Processes:

  • [1 mark]Initialising three variables (Count, Number, Total)
  • [1 mark]Inputting a user number and adding to two variables (Total, Count)
  • [1 mark]Repeating nine more times, then outputting the final Total value
Answer
INPUT Age
IF Age >= 18 THEN
OUTPUT "Welcome to the site"
ELSE
OUTPUT "Sorry, this site is for users 18 and over"
ENDIF

Marking:INPUT statement (1), correct IF condition (1), correct THEN output (1), correct ELSE output (1), ENDIF (1).

Answer
  • [1 mark]A well-designed algorithm can be interpreted by a new user who should be able to explain what it does
  • [1 mark]Following the algorithm instructions should make its purpose clear
  • [1 mark]If the algorithm is complex, the user can look for comments, consider the context of where it is used, or test it with different inputs
Answer
  • [1 mark]Five marks are input and added to a running total
  • [1 mark]The total is divided by 5 to calculate the average
  • [1 mark]The average is output to the user
Answer
  • [1 mark]Pseudocode is a simplified form of programming code that does not worry about the actual rules and syntax of a particular language
  • [1 mark]Using real code syntax such asprint()orinput()with brackets shows you are not using the standard pseudocode style
  • [1 mark]The exam expects simple statements likeINPUT AgeandOUTPUT "Welcome"— adding actual language syntax can lose you marks because it is not the correct pseudocode format