ALGORITHMS, FLOWCHARTS & SUB-ROUTINES
Algorithm
It is a set of logical steps to perform a task.
It is a program design which can be converted, translated or mapped to a particular
programming language such as Python, Java and VB.NET.
It does not run on the computer until it is converted into a particular programming
language.
Variable
It is an identifier that stores a value used by a program in computer memory.
Computer memory is divided into thousands of addressable storage units or locations.
A variable is assigned or allocated to one or more such memory locations.
Examples of variables
A=10
M=50-45
average=sum/n
Total=Number1+ Number2
Ways of expressing algorithms
flowcharts
psuedocode
Flowchart
It is a visual representation of an algorithm using standard symbols.
Standard flowchart symbols
Symbol Meaning
Start or stop
StProcess
Input/output 1
Decision/branching/selection
Data flow/connector
Subroutine/routine/module
Operators
Arithmetic
Comparison
Arithmetic operators
These are symbols used to perform mathematical calculations in a program.
Symbol Meaning Example
+ addition y=x+z
- subtraction A=B-C
* multiplication total=quantity*price
/ division Average=Sum/N
Comparison operators
These are symbols used to compare two or more values and this comparison always yields a
Boolean result.
A Boolean result is either true or false.
INPUT, OUTPUT and sequence statements
INPUT statement/command
An instruction that inputs data into a program from the user and stores it in a variable.
Examples
INPUT A
INPUT B
OUTPUT statement/command
An instruction that outputs data from a program to the screen/monitor.
Examples
OUTPUT X
2
OUTPUT sum
OUTPUT “Total= ” + Total
Sequence statement
A group of program statements that are executed one after the other from top to bottom in
the order they are written.
Examples
INPUT a
INPUT b
c=a-b
d=c*b
OUTPUT c, d
Flowchart example 1
Below flowchart algorithm performs movement actions.
Flowchart example 2
Below flowchart inputs two numbers, calculate their average and output that average.
3
Q: Input different pairs of numbers into the algorithm and write down the expected output.
Changing, modifying, amending or editing an algorithm
An algorithm may need to be modified for the following reasons.
To remove an error or bug
To make the algorithm do an extra task
When its purpose has changed
When it is being adapted from a flowchart used for another task
Example 1
What the flowchart is doing
It inputs two numbers, then subtracts the second number from the first and outputs the
result.
What the flowchart was supposed to do
Input two numbers, multiply them together and output the result.
4
Q: Find the error in above flowchart and suggest the change needed.
A: The instruction/command/statement in the process box must be either:
result=first x second
Or
result=first*second
Example 2
Q: Identify the error in the flowchart.
A: The algorithm does not produce an output as it is asked to calculate the “average” ,
but then outputs the “result”. The “result” variable has not been defined for the algorithm
to know what the variable “result” should be.
Thus, one of either the “Average” variable or the “Result” variable needs to be changed
so that it matches the other.
Example 3
Below flowchart needs your name, age and favourite colour as input and to output a
message that uses all three of these.
5
Q: Read through the algorithm and discuss:
Whether it contains an error or not.
If so, what the error is and where it occurs.
Suggest how to fix the error.
A: The colour is not taken as input so this is an error. Thus an extra input is required for
“INPUT colour” and must come before the output box.
Example 4
Below algorithm should take three numbers as input, add them together and outputs the
result.
Q: Follow the algorithm to see how it works.
New requirement
An additional feature is needed within the algorithm so that it outputs a message that
includes the sum within the output, for example, “1+2+2=5” .
Q: How would you change the flowchart to meet this new requirement?
6
A: Create a new version of the flowchart with this amendment included. The output
statement must now be:
OUTPUT number1 + ”+” + number2 + ”+” number3 + “=” + result
Practice Questions
1)Below flowchart asks for the user’s favourite colour.
Ss Start
INPUT FavColour
OUTPUT FavColour
Sps Stop
Q: Amend the algorithm so that it also asks for the user’s favourite TV show and output
both the favourite colour and the TV show.
7
2. Below flowchart tells the user to move in a square.
Ss Start
0
Turn right 90 bb
Move forward 10 stepsb
0
Turn right 90 bb
Move forward 10 stepsb
0
Turn right 90 bb
Move forward 10 stepsb
0
Turn right 90 bb
Move forward 10 stepsb
ssStop
Amend the algorithm so that it tells the user to move in a rectangle instead.
3. Below algorithm asks the user for their pet’s name and output it. Amend it so that it asks the user
the type of pet before the pet’s name. It must then output both the pet’s type and the pet’s name.
Ss Start
INPUT PetName
OUTPUT PetName
Sp sStop