Algorithms and Flowcharts
Algorithms and Flowcharts
Algorithms
An algorithm is a set of instructions sequenced to solve a problem or to
represent a calculation. Some would say that an algorithm is basically a
data or program flowchart without the boxes. It is actually a list of
precise steps.
The order in which these steps are carried out is always crucial to the
way an algorithm works. We start at the first line of the algorithm and
work downwards to the final instruction.
Algorithms can be written in many different ways, including everyday
natural language, pseudocode, flowcharts or programming languages.
Z←W+X+Y
• This assigns the result of adding the contents of W, X and Y together
and stores the result of this calculation in Z.
Algorithm to multiply two numbers on a
calculator
Or can be written as
Conditional branching (IF…THEN…ELSE)
• This is sometimes referred to as selection. Conditional branching
means that certain statements are carried out depending on the value
stored within a particular variable name. There are generally
considered to be two types, which are IF…THEN…ELSE and CASE…
ENDCASE.
IF…THEN…ELSE
Exercise: Write an Algorithm using
IF..THEN..ELSE
Consider an exam system where if a student gains 75 or more marks
they are awarded a distinction; if they get between 60 and 74 marks
they are awarded a merit; between 40 and 59 marks the student is
awarded a pass; otherwise they fail the course.
Exercise
Write an algorithm using IF…THEN…ELSE condition, which would input
10 exam marks and if the AVERAGE mark is greater than 50 the output
would be ‘You have passed’. If it was not, then the output would be
‘You have not reached the pass mark’.
Example
Exercise
Write an algorithm that inputs two numbers, checks to see if the
second number is bigger than the first or otherwise, divides bigger
number by smaller numbers and outputs the answer.
CASE…ENDCASE
• With this type of condition, depending on the value input, the
algorithm carries out one of a number of statements.
Example which includes an OTHERWISE
statement.
Loops
• A loop is a sequence of repeated statements that are carried out a
number of times.
WHILE…ENDWHILE
An example of a WHILE…ENDWHILE
algorithm
Question
• Write an algorithm using WHILE…ENDWHILE conditions, which would
input 10 exam marks and if the mark was greater than 40 the output
would be ‘You have passed’. If it was not, then the output would be
‘You have not reached the pass mark’.
REPEAT…UNTIL
Example
Central-heating systems: Steps Involved
1- User enters required temperature using keypad/ touchscreen
2- Microprocessor stores required temperature as a pre-set value
3- Microprocessor receives temperature from sensor
4- Microprocessor compares temperature from sensor to pre-set value
5- If temperature from the sensor is lower than the value, the
microprocessor sends a signal to an actuator to open the gas valves
6- If temperature from the sensor is lower than the pre-set value, the
microprocessor sends a signal to an actuator to switch the pump on
7- If temperature is higher than or equal to the preset value the
microprocessor sends a signal to switch the pump off and close the
valves
8 - This sequence is repeated until the system is switched off
Flowchart describing the processing carried out
by the microprocessor in a central heating
system
Common Symbols
The flowchart to add up five numbers
Task 1
• Draw a flowchart which will allow the average of six numbers to be
calculated.
• Write out the steps in the operation of an air-conditioning system,
putting each distinct step on a separate line.
• Draw a flowchart for an air-conditioning system which could describe
the processing carried out by the microprocessor, assuming the pre-
set (required) temperature has already been input to the system.