Algorithms
Algorithms
An algorithm is a sequence of logical instructions for carrying out a task. In computing, algorithms
are needed to design computer programs.
Designing an algorithm
An algorithm is a plan, a logical step-by-step process for solving a problem. Algorithms are
normally written as a flowchart or in pseudocode.
A flowchart is a diagram that shows a process, made up of boxes representing steps, decisions,
inputs and outputs.
Pseudocode is a method of writing up a set of instructions for a computer program using plain
English. This is a good way of planning a program before coding.
When designing an algorithm there are two main areas to look at:
Before an algorithm can be designed, it is important to check that the problem is completely
understood. There are a number of basic things to know in order to really understand the problem:
Before designing an algorithm it is important to first understand what the problem is. Algorithms
can be designed using pseudocode or a flowchart, and the standard notations of each should be
known.
Pseudocode
Most programs are developed using programming languages. These languages have
specific syntax that must be used so that the program will run properly. Pseudocode is not a
programming language. It is a simple way of describing a set of instructions that does not have to
use specific syntax.
There is no strict set of standard notations for pseudocode, but some of the most widely
recognised are:
Using pseudocode
Pseudocode can be used to plan out programs. Planning a program that asks people what the
best subject they take is, would look like this in pseudocode:
REPEAT
OUTPUT 'What is the best subject you take?'
INPUT user inputs the best subject they take
STORE the user's input in the answer variable
IF answer = 'Computer Science' THEN
OUTPUT 'Of course it is!'
ELSE OUTPUT 'Try again!'
UNTIL answer = 'Computer Science'
Flowcharts
A flowchart is a diagram that represents a set of instructions. Flowcharts normally use standard
symbols to represent the different types of instructions. These symbols are used to construct the
flowchart and show the step-by-step solution to the problem.
Common flowchart symbols
Using flowcharts
General example of a flowchart structure to illustrate the use of the various symbols:
Another example of a flowchart is that of asking people what the best subject they take
is, would look like this as a flowchart:
Up until now, there is a symbol in drawing up algorithms that we have not used: the diamond
shape:
The diamond shape is used to make decisions, and has one input and 2 outputs. The outputs are
binary, ie either TRUE or FALSE or YES or NO. The complete structure of the decision block is:
Decision control structures are used when the next step to be executed depends upon a criterion.
This criterion is usually one or more Boolean expressions that must be evaluated. A Boolean
expression always evaluates to “true” or “false”. One set of statements is executed if the criteria
is “true” and another set executed if the criteria evaluates to “false”.
The decision structure is also used in repetition control or loop. Repetition control structure is
used when a set of statements in to be repeated many times. The number of repetitions might be
known before it starts or may depend on the value of an expression.
A simple everyday example of the use of a decision structure when we want to go to the shop to
buy a pen is shown in the figure below:
Notice that the decision structure has two outputs, which can either be TRUE or FALSE. If TRUE
then one set of actions is carried. If FALSE, then a different set of actions is carried out.
A number is even if it can be divided by 2 without remainder. Such numbers are 2, 4, 6, 8.. and
so on. The numbers that leave a remainder are called odd. They are 1, 3, 5, 7.. and so on.
remainder = n 2
n 2
In a repetition loop, we carry out the same set of activities until a condition is met.
Example: In the previous notes we found the sum of two numbers. How do we find the sum of the
first 50 numbers?
Summing two numbers was easy, but how about 50? Do you have to write 50 blocks to solve this
task? No,