Algorithms and Flowcharts
Algorithms and Flowcharts
Flowcharts
Chapter 4
Information Technology 9626 – Chapter 4 30/03/2022 2
Objectives
• write a basic algorithm that demonstrates a decision-making process.
• use conditional branching within an algorithm.
• use loops within an algorithm.
• use nested loops within an algorithm.
• include procedures/subroutines within an algorithm.
• edit a given algorithm
• write an algorithm to solve a given problem.
• edit a given flowchart.
• draw a basic program flowchart that demonstrates a decision-making process.
• draw a program flowchart to solve a given problem.
• identify errors in an algorithm or program flowchart for a given scenario.
Information Technology 9626 – Chapter 4 30/03/2022 3
Program
• It is a set of instruction that tells the computer what to do, computer works
according to the given instructions in the program
• Written in a programming language (C++, Python, Java)
• Written by programmer
Information Technology 9626 – Chapter 4 30/03/2022 4
pseudocode
Algorithm Final
Program
Flowchart
Information Technology 9626 – Chapter 4 30/03/2022 5
Algorithm
• set of instructions sequenced to solve a problem or to represent a calculation
• a data or program flowchart without the boxes.
• 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.
• Input -> Process -> Output
• Data -> Process -> Information.
Information Technology 9626 – Chapter 4 30/03/2022 6
Algorithm
Algorithm
Everyday Life example:
Recipe for baking a cake
Information Technology 9626 – Chapter 4 30/03/2022 9
Algorithm
Computer based example
Advantages of Algorithms
Maze 1
FD7,RT90,FD6,LT90,FD2,RT90,FD1
Information Technology 9626 – Chapter 4 30/03/2022 13
Maze 2
Maze 3
FD3
RT90
FD4
LT90
FD
RT90
FD4
RT90
FD4
RT90
FD4
LT90
FD3
LT90
FD5
LT90
FD2
Information Technology 9626 – Chapter 4 30/03/2022 15
Questions
Design of Algorithm
• An algorithm must be carefully designed.
• Problem should be broken down into simple and meaningful steps
• Steps should be descriptive and written in simple english
• Should caters for all possible scenarios.
• Any steps that rely on decisions being made must be dealt with in sequence and
the conditions must be clear and unambiguous.
• not all instructions may be carried out for a particular scenario, but they still have
to be written in a way that allows for all possible scenarios.
Information Technology 9626 – Chapter 4 30/03/2022 17
Pseudocode
Pseudocode
• Pseudocode consists of short, readable and formally styled English languages used for
explain an algorithm.
• It does NOT include details like variable declaration, subroutines.
• It is easier to understand for the programmer or non programmer to understand the
general working of the program, because it is not based on any programming language.
• It gives us the sketch of the program before actual coding.
• It is not machine readable. (Computer cannot understand it)
• Pseudo code can’t be compiled and executed.
• There is no standard syntax for pseudo code.
Information Technology 9626 – Chapter 4 30/03/2022 20
Advantages of Pseudocode
Disadvantages of Pseudocode
Flowcharts
Structure of Flowcharts
Start
Stop
Information Technology 9626 – Chapter 4 30/03/2022 24
I/O
• Input
INPUT INPUT x
• Output
No knowledge of programming
User needs knowledge to write No knowledge of program to
language needed to understand
algorithm draw or understand flowchart
or write a pseudocode
Information Technology 9626 – Chapter 4 30/03/2022 26
INPUT X,Y,Z
Line numbers
Key Actions
key actions that are performed and common terms used in pseudocode:
Variable
Arrow symbol =
Information Technology 9626 – Chapter 4 30/03/2022 32
Most commonly used names are x,y,z although its always better to name
it appropriately, according to the value it will used for (marks, number,
average etc)
X = 109
Addition = X + Y + Z
Information Technology 9626 – Chapter 4 30/03/2022 33
Naming variables
Example
Problem: An algorithm needs to ask a user to enter their name. It should take their name as an input and then welcome
them by name for example “Welcome Sasha”
Draw a flow chart for the algorithm
Algorithm steps:
• Ask them to enter their name
• Take their name as input.
• Welcome them by their name
• Identify the types of the symbols
• Ask them to enter their name (output)
• Take their name as input (input and store in variable )
• Welcome them by their name (output including variable)
• Draw the flow chart following the steps identified.
• Write the pseudocode
Information Technology 9626 – Chapter 4 30/03/2022 35
Pseudocode Flowchart
Start
OUTPUT “Enter your name”
INPUT name
OUTPUT “Welcome” & name
Stop
Information Technology 9626 – Chapter 4 30/03/2022 36
Processes
Process
Information Technology 9626 – Chapter 4 30/03/2022 37
Processes
Operator Description Example
+ Addition myNumber = 10 + 3
– Subtraction myNumber = 10 – 3
* Multiplication myNumber = 10 * 3
/ Division myNumber = 10 / 3
^ Power of myNumber = 10 ^ 3
Example 2
Example 2 Pseudocode
BASIC program
Theoretical Questions
5) What is a process?
6) What action does the ^ operator process?
7) Write a statement to multiply 3 by 7
8) Describe the stages in this flowchart.
Information Technology 9626 – Chapter 4 30/03/2022 43
Practical Activity
= Equals to 10 = 5 is false
10 = 10 is true
Selection in Flowchart
• Single selection
• The selection must be in the form of a question, for example ‘is 10 >
5?’ This is because it has two results: true and false.
• The selection flowchart symbol is a diamond.
No YE
DECISION S
Information Technology 9626 – Chapter 4 30/03/2022 48
Information Technology 9626 – Chapter 4 30/03/2022 49
IF – THEN - ELSE
IF <condition>
THEN
<statement or list of statements to be carried out >
ELSE
<alternative statement or list of alternative
statements to be carried out>
ENDIF
Information Technology 9626 – Chapter 4 30/03/2022 51
Multiple Decisions
Decision Making
Information Technology 9626 – Chapter 4 30/03/2022 54
IF
Information Technology 9626 – Chapter 4 30/03/2022 55
IF - Else
Information Technology 9626 – Chapter 4 30/03/2022 56
IF – Else IF
Information Technology 9626 – Chapter 4 30/03/2022 57
Nested IF
Information Technology 9626 – Chapter 4 30/03/2022 58
Nested IF
Information Technology 9626 – Chapter 4 30/03/2022 59
Flowchart Is Mark<
No
OUTPUT “Congratulations you have
75 ? been awarded a distinction”
YES
Is Mark< No
OUTPUT “Well done. You have been
60 ? awarded a merit”
YES
Is Mark<
No
OUTPUT “You have passed”
40 ?
YES
SELECT CASE
SELECT <identifier>
CASE <value 1>
<statement or list of statements>
CASE <value 2>
<statement or list of statements>
DEFAULT
<statement or list of statements>
Information Technology 9626 – Chapter 4 30/03/2022 63
CASE Flowchart
Information Technology 9626 – Chapter 4 30/03/2022 64
Constant values
Default Case
There is also a default keyword for CASE, the code in this will run if none of the conditions have been met.
CASE Example
INPUT grade
SELECT grade
CASE ‘A’ :
X = 75
Y = 100
CASE ‘B’ :
X = 60
Y = 74
CASE ‘C’ :
X = 59
Y = 40
CASE‘F’ :
X = 39
Y = 0
PRINT "Your mark must have been between " X “and " Y
Information Technology 9626 – Chapter 4 30/03/2022 67
Information Technology 9626 – Chapter 4 30/03/2022 68
Work it out!
Variable values
A CASE statement can perform comparisons other than equals to, but this time you need to
include the variable name to make it clear what the comparison is.
SELECT age
CASE age < 12: OUTPUT "You can watch PG films"
CASE (age >= 12 and age < 15): OUTPUT "You can watch PG and PG12 films"
CASE (age >= 15 and age < 18): OUTPUT "You can watch PG, PG12 and 15 rated films"
CASE age >= 18: OUTPUT "You can watch all films"
Information Technology 9626 – Chapter 4 30/03/2022 70
Practical Activity
1 Describe the stages in the flowchart opposite.
2 Convert the flowchart in part 1 into pseudocode.
3 An algorithm needs to take three numbers as input, and output the smallest.
Draw a flowchart for this algorithm.
4 Convert your flowchart from part 3 into pseudocode.
5 An algorithm needs to take two numbers as input, then subtract the smallest from
the largest.
Create a pseudocode algorithm for the problem using an IF statement.
6 Replace the IF statement in task 5 with a SELECT CASE statement.
Information Technology 9626 – Chapter 4 30/03/2022 71
Loops
• A loop means repetition; doing the same thing several times. Its
technical term is iteration.
• Loops are mainly used in two scenarios:
• You know how many times you want to repeat the code. This might be 10
times, or age times (where age is a variable that stores a value). This is
a count-controlled loop.
• You don’t know the number of times, but want to keep going until
something happens. This is a condition-controlled loop
Information Technology 9626 – Chapter 4 30/03/2022 72
Count controlled
Flowchart
Information Technology 9626 – Chapter 4 30/03/2022 74
Pseudocode
• 1) counter is set to 10
count = 10
• LOOP through count
from 10 to 0
WHILE count >=0
• Output the counter
OUTPUT count
value. count= count – 1
• Decrement the count ENDWHILE
value by 1
• When the value of count
becomes less than 0,
then end the loop
FOR count = 10 to 0
OUTPUT count
NEXT count