You are on page 1of 17

What is an Algorithm

 We use algorithms in everyday life. If you need to change a wheel on a car,


you might need to follow instructions (the algorithm) from a manual.
1. Take a spanner and loosen the wheel nuts.
2. Position a jack in an appropriate place.
3. Raise the car.
4. Take off the wheel nuts and the wheel.
5. Lift replacement wheel into position.
6. Replace wheel nuts and tighten by hand.
7. Lower the car.
8. Fully tighten wheel nuts.
 This might sound all very straightforward. However, if the instructions are
not followed in the correct logical sequence, the process might become
much more difficult or even impossible.
 For example, if you tried to do Step 1 after Step 3, the wheel may spin and
you can’t loosen the wheel nuts. You can’t do Step 4 before Step 3.
Example of Algorithm
 If you want to bake a cake, you follow a recipe.
 1 Measure the following ingredients: 200g sugar, 200g butter, 4 eggs, 200g
flour, 2 teaspoons baking powder and 2 tablespoons of milk.
 2 Mix the ingredients together in a large bowl, until the consistency of the
mixture is smooth.
 3 Pour the mixture into a cake tin.
 4 Bake in the oven at 190° C for 20 minutes.
 5 Check it is fully cooked.
 6 Turn cake out of the tin and cool on a wire rack.
 The recipe is an algorithm. The ingredients are the input and the cake is the
output. The process is mixing the ingredients and cooking the mixture in the
oven.
 Sometimes a step might need breaking down into smaller steps. For example,
Step 2 can be more detailed.
2.1 Beat the sugar and butter together until fluffy.
2.2 Add the eggs, one at a time, mixing constantly.
2.3 Sieve the flour and baking powder and stir slowly into the egg mixture.
2.4 Add milk and mix to give a creamy consistency.

 Sometimes there might be different steps depending on some other conditions.


For example, consider how to get from one place to another using the map of the
London Underground system in Figure 12.02.
 To travel from King’s Cross St. Pancras to Westminster, we consider two routes:
•• Route A: Take the Victoria Line to Green Park (4 stations); then take the Jubilee
Line to Westminster (1 station)
•• Route B: Take the Piccadilly Line to Green Park (6 stations); then take the Jubilee
Line to Westminster (1 station).
 Route A looks like the best route. If there are engineering works on the Victoria Line and
trains are delayed, Route B might turn out to be the quicker route.
 The directions on how to get from King’s Cross St. Pancras to Westminster can be written
as:
 IF there are engineering works on the Victoria Line
THEN
Take the Piccadilly Line to Green Park (6 stations)
Take the Jubilee Line to Westminster (1 station)
ELSE
Take the Victoria Line to Green Park (4 stations)
Take the Jubilee Line to Westminster (1 station)
Writing algorithms that provide
solutions to problems
 There are several methods of writing algorithms before attempting to program a solution.
Here are three frequently used methods.
 Structured English
 Flowchart
 Pseudocode
 • Structured English is a method of showing the logical steps in an algorithm, using an agreed
subset of straightforward English words for commands and mathematical operations to
represent the solution. These steps can be numbered.
 • A flowchart shows diagrammatically, using a set of symbols linked together with flow lines,
the steps required for a task and the order in which they are to be performed. These steps,
together with the order, are called an algorithm. Flowcharts are an effective way to show the
structure of an algorithm.
Writing algorithms that provide
solutions to problems
 • Pseudocode is a method of showing the detailed logical steps in an algorithm,
using keywords, identifiers with meaningful names and mathematical operators
to represent a solution.
 Pseudocode does not need to follow the syntax of a specific programming
language, but it should provide sufficient detail to allow a program to be written
in a high-level language.
Writing simple algorithms using
pseudocode
 Each line of pseudocode is usually a single step in an algorithm.
 All identifier names used in pseudocode should be meaningful; for
example, the name of a person could be stored in the variable identified
by Name.
 They should also follow some basic rules:
 they should only contain the characters A–Z, a–z and 0–9, and should
start with a letter.
 Pseudocode identifiers are usually considered to be case insensitive,
unlike identifiers used in a programming language, but is
recommended to use Camel case (First letter capital, no space in
between if more than one word) for them
Writing simple algorithms using
pseudocode
 It is good practice to keep track of any identifiers used, in an identifier table
Pseudocode statements used for writing
algorithms.
 INTPUT is used for data entry
 It is followed by a variable where data input is stored
 Example:
To display output
Assignment Statements

 To assign a value to a variable (the value can be the result of a process or a


calculation):
Assignment Operators

 Operators used in pseudocode assignment statements:


Selection

 Selection can be done by using:


 IF statement:
 IF…THEN…ENDIF

 IF…THEN…ELSE…ENDIF

 CASE Statement:
 CASE…ENDCASE

 CASE…OTHERWISE…ENDCASE
IF Statement

 To perform a selection using IF statements for a single choice or a


choice and an alternative:
Case Statement
 For a CASE statement, the value of the variable decides the path to be taken.
 Several values are usually specified.
 OTHERWISE is the path taken for all other values
 The end of statement is shown by ENDCASE
 To perform a selection using CASE statements when there are multiple choices
or multiple choices and an alternative: (The variable used here is Direction)
Relational Operators

 Relational operators used in pseudocode selection statements:


= Equal to
<> Not equal to
> Greater than
> Less than
>= Greater than or equal to
<= Less than or equal to

You might also like