Algorithm and Flowcharting
Algorithm and Flowcharting
1
Algorith
m
• An algorithm is a plan, a set of step-by-step instructions to
resolve a problem.
• In an algorithm, each instruction is identified and the order
in which they should be carried out is planned.
• Algorithms are one of the four cornerstones of Computer
Science. Click to add text
If you can tie shoelaces, make a cup of tea, get dressed or
prepare a meal then you already know how to follow an
algorithm.
• In an algorithm, each instruction is identified and the order in
which they should be carried out is planned.
• Algorithms are often used as a starting point for creating a
computer program, and they are sometimes written as a
2
flowchart or in pseudocode.
Instruct a
computer
• If we want to tell a computer to do something, we have to
write a computer program that will tell the computer, step-by-
step, exactly what we want it to do and how we want it to do
it.
• This step-by-step program will need planning, and to do
this we use an algorithm.
• Computers are only as good as the algorithms they are given.
• If you give a computer a poor algorithm, you will get a poor
result – hence the phrase:‘Garbage in, garbage out.’
• Algorithms are used for many different things including
calculations, data processing and automation.
3
Plan for
•solution
It is important to plan out the solution to a problem to make
sure that it will be correct.
• Using computational thinking and decomposition we can
break down the problem into smaller parts and then we can
plan out how they fit back together in a suitable order to
solve the problem.
• This order can be represented as an algorithm.
• An algorithm must be clear.
• It must have a starting point, a finishing point and a set of
clear instructions in between.
4
Representing
algorithms
• There are two main ways that algorithms can be
represented –
pseudocode and flowcharts.
• Most programs are developed using programming languages.
• These languages have specific syntax that must
be used so that the program will run properly.
8
Relationship between rules and
algorithms
Not all lists of rules are algorithms.
Example: Rules on toilet of in an airliner.
• No smoking.
• No eating.
• Do not use when the fasten seat belt sigh is lit.
• But the following rules in an airliner exit door is an
algorithm,
Emergency opening:
1. Pull cover aside.
2. Push lever to open position and release.
3. Push door outwards.
10
A simple command like 'spread butter on
bread‘
• Remove lid from butter tub and place to one side.
• Place 5 grams of butter on the tip of the knife.
• Position the tip of the knife on the upwards side of the slice
of bread with the butter between the knife and the bread.
• Move the knife backwards and forwards in a sweeping
motion across the bread to spread it at an even thickness.
• Repeat steps 2 to 4 until one side of the slice of bread is
evenly coated with butter.
• Remove any excess butter from the tip of the knife.
11
12
A simple program could be created to ask someone
their name and age, and to make a comment based
on these.
This program represented in pseudocode would
look like'What
OUTPUT this:is your
name?‘ INPUT user inputs
their name
STORE the user's input in the name
variable OUTPUT 'Hello' + name
OUTPUT 'How old are you?‘
INPUT user inputs their age STORE the user's input in the age
variable IF age >= 70 THEN OUTPUT 'You are aged to
perfection!‘
ELSE OUTPUT 'You are a spring chicken!'
14
An Algorithm for dealing with a non-
functioning bulb
Pseudocode Flowch
If the lamp plugged art
in Then
If the bulb burned
out Then
Replace
bulb Else
Buy
Figure: Flowchart for
dealing with non-
new lamp functioning bulb
Else Did you notice different shapes/symbols in the
Plugin the flowchart?
Algorithms are about HOW we perform
something.
22
Selectio
n
• IF the door is locked, THEN unlock the
door,
ELSE do nothing (go to next
instruction)
• IF the door is closed, THEN open the
door,
ELSE do nothing
• Enter the room
• IF the room is dark, THEN switch
on the light, ELSE do nothing
• Close the door behind you
23
Iteratio
n
24
Flowchart for classifying
angles
Can you fill the flowchart with appropriate angles? (reflex
angle, obtuse angle, angle on straight line, right angle, acute
angle).
• Algorithm B
Ask the user
to type in a
whole
number
Add 5 to that
number
Print the
• result
Algorithm D
Ask the user to type in a whole number
• Algorithm C typed in is less than 5
If the number
If 4 is Add
less 5 to the number typed in Print the
than
result5 Add 5
Otherwise Print "no"
28 to 4
Sequence
structure
• Sequence
• Series of actions performed in
sequence.
• Selection
• Selecting one of
two possible
actions
depending on a
condition.
Figure: Green
Jeep
Drawing a Off-Road Vehicle-
Level1
STEP-1: Draw a green grille.
What is evaluation?
• Once a solution has been designed using
computational thinking, it is important to make sure that
the solution is fit for purpose.
• Evaluation is the process that allows us to make sure our
solution does the job it has been designed to do and to think
about how it could be improved.
58
Make sure that the solution is fit for
purpose………………….. How ????
59
Evaluate the
algorithm
Once written, an algorithm should be checked to make sure it:
• is easily understood – is it fully decomposed?
• is complete – does it solve every aspect of the problem?
• is efficient – does it solve the problem, making best use of
the available resources (eg as quickly as possible/using
least space)?
• meets any design criteria we have been given
• If an algorithm meets these four criteria it is likely to work
well.
• The algorithm can then be programmed.
61
Faulty solutions do not solve the
•problem
Once a solution has been decided and the algorithm designed,
it can be tempting to miss out the evaluating stage and to
start programming immediately.
However, without evaluation any faults in the algorithm will
not be picked up, and the program may not correctly solve
the problem, or may not solve it in the best way.
• Faults may be minor and not very important.
• For example, if a solution to the question ‘how to draw a
cat?’ was created and this had faults, all that would be
wrong is that the cat drawn might not look like a cat.
• However, faults can have huge – and terrible – effects, eg if
the solution for an aeroplane autopilot had faults.
62
A faulty solution may include one or more of these
errors.
• We may find that solutions fail because:
• it is not fully understood - we may hav
not properly decomposed the problem e
63
A study of “Evaluation” in detail
• Consider the example “How to bake a cake”
• It is decomposed to:
• what kind of cake to bake
• what ingredients are needed, how much of each
ingredient, and when to add it
• how many people the cake is for
• how long to bake the cake for
• what equipment is needed
64
#1 Solutions that are not properly decomposed
The Decomposition of
equipment
65
Equipment Decomposition is
incomplete
• The ‘Equipment’ part is not properly broken down (or
decomposed).
• Therefore, if the solution - or algorithm – were created from
this, baking the cake would run into problems.
• The algorithm would say what equipment is needed, but not
how to use it, so a person could end up trying to use a knife
to measure out the flour and a whisk to cut a lump of butter,
for example.
• This would be wrong and would, of course, not work.
• Ideally, then, ‘Equipment’ should be decomposed further, to
state which equipment is needed and which ingredients each
item is used with.
66
The problem occurred here because the problem of which
equipment to use and which ingredients to use it with
hadn’t been fully decomposed.
67
# 2 Solutions that are incomplete
• How to bake a cake decomposition is still – part of the
incomplete
problem has been left out. We still need to know:
• where to bake the cake
• what temperature
• Therefore, to bake thewas
if this information cakeused
at to create the solution, the
algorithm would say how long the cake should be baked for but it
would not state that the cake should be placed in the oven, or the
temperature that the oven should be.
• Even if the cake made it to the oven, it could end up undercooked
or burnt to a cinder.
• Very important factors have been left out, so the chances of making
a great cake are slim.
The problem occurred here because placing the cake in the oven
and specifying the oven temperature had not been included,
making the solution incomplete.
68
69
#3 Solutions that are inefficient
• The solution would state – among other things – that
certain quantities of particular ingredients are needed to
make the cake.
70
While this would work, it would be unnecessarily long
and complicated
71
It would be more efficient to fetch all the ingredients in one
go, and the program would be shorter as a result
The solution is now simpler and more efficient, and has reduced
from nine steps to five.
The problem occurred here because some steps were repeated
unnecessarily, making the solution inefficient and overly long.
72
#4 Solutions that do not meet the original design criteria
73
• For example, the cake may have to be a chocolate cake, which
is still quite general, or a chocolate fudge cake with chocolate
icing and flakes on top, which is more specific.
75
(2) Does the solution cover all parts of the problem?
• For example, if drawing a cat, does the solution
describe everything needed to draw a cat, not just eyes, a
tail and fur?
• If not, go back and keeping adding steps to the solution
until it is complete.
• With pen and paper, work through the algorithm and trace a
path through it.
77
A simple algorithm was created to ask someone their name
and age, and to make a comment.
You could try out this algorithm – give it a dry run.