Algorithms, Flowcharts and Subroutines Notes 1
Algorithms, Flowcharts and Subroutines Notes 1
Algorithm
It does not run on the computer until it is converted into a particular programming
language.
Variable
Examples of variables
A=10
M=50-45
average=sum/n
Total=Number1+ Number2
flowcharts
psuedocode
Flowchart
It is a visual representation of an algorithm using standard symbols.
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.
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 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.
Example 1
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
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.
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:
Practice Questions
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
0
Turn right 90 bb
0
Turn right 90 bb
0
Turn right 90 bb
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