Max Algorithm - Mr Lyon Computing

Interpreting and Creating
Algorithms
What is an Algorithm?
An algorithm is a set of instructions that can be followed to solve a
problem.
Here is a simple algorithm for making toast:
Slice the bread
Toast the bread
Butter the toast
Spread the jam
Flowcharts
Flowcharts can be used to represent algorithms in the form of a diagram.
A number of standard symbols are used in flowcharts:
Start/Stop
Process
Used to indicate the start or end of an
algorithm.
Used to indicate a process, for example
performing a calculation.
Decision
Input /
Output
Used to control the path taken through
an algorithm based on the result of a
condition.
Used when data needs to be inputted or
outputted.
Example Flowchart
This is an example algorithm represented using a flowchart, it is designed
to calculate the average of a list of values.
Start
List
5
6
4
Total = 0
Item = 1
Is Item
<=
Length
of list?
Yes
Total = Total +
List[Item]
Item = Item + 1
No
Average =
Total/Length of list
OUTPUT
Average
End
Total
11
20
15
50
Item
23541
Average
5
Output
5
5
Pseudocode
Pseudocode is a way of designing an algorithm using a syntax that looks
similar to a real programming language.
There are no specific rules to writing pseudocode, the important thing is
to be consistent.
SET Total TO 0
SET Item TO 1
SET ListLength TO Length[List]
WHILE Item <= ListLength DO
SET Total = Total List[Item]
SET Item = Item + 1
END WHILE
SET Average TO Total/ListLength
SEND Average TO DISPLAY
List
5
6
4
Total
20
15
0
5
11
Item
4
2
531
ListLength
4
Average
5
Output
5
5
Flowchart Activity
Design a flowchart to add up these 6 numbers and output the total:
Start
List
Total = 0
Item = 1
Is Item
<=
Length
of list?
No
OUTPUT
Total
Yes
Total = Total +
List[Item]
Item = Item + 1
End
25
6
8
5
6
4
6
4
5
Pseudocode Activity
Using pseudocode create the program you have written on the previous
slide.
SET Total TO 0
SET Item TO 1
SET ListLength TO Length[List]
WHILE Item <= ListLength DO
SET Total = Total List[Item]
SET Item = Item + 1
END WHILE
SEND Total TO DISPLAY
Written Description
Another method of representing an algorithm is to write a written
description of what it needs to do in plain English.
For example:
Go through the list adding together each value, once finished divide the
total by the length of the list and output it.
Program Code
Program code is the form an algorithm takes when it is turned into a
working program using a programming language.
For example, here is
the average
algorithm written in
the Python
programming
language:
Standard Algorithms
Standard Algorithms
There are a number of common tasks that need to be carried out
regularly when programming.
These tasks include:
Counting the number of occurrences of a value
Finding the lowest value
Finding the highest value
Finding the mean
Count Algorithm
The count algorithm is used to count the number of occurrences of a
value in a list.
4, 6, 8, 14, 18, 6, 15, 7, 6, 17, 12, 6, 15, 19, 20, 3, 15, 6
Count the number of occurrences of the number 6 in this list.
How did you come up with your answer?
Input the number to find
Go through the list one element at a time
If the current element = 6 then add 1 to the total
Output the total
Count Program
This is a partially complete version of the count program written in
Python.
You need to complete the program by replacing the comments with
appropriate lines of code.
Comment the code.
Count Program Solution
Max Algorithm
The max algorithm is used to find the highest value from a list of values.
4, 6, 8, 14, 18, 6, 15, 7, 6, 17, 12, 6, 15, 19, 20, 3, 15, 6
Find the highest value from this list of values.
How did you come up with your answer?
Set highest to the first value in the list
Go through the list one element at a time
If the current element is larger than highest
replace highest with the value of the current element
Output highest
Max Program
This is a partially complete version of the max program written in Python.
You need to complete the program by replacing the comments with
appropriate lines of code.
Hint: Look at the data containers lesson
Hint: > is greater than
Comment the code.
Max Program Solution
Min Algorithm
The min algorithm is used to find the lowest value from a list of values.
2, 6, 8, 14, 18, 6, 15, 7, 6, 17, 12, 6, 15, 19, 20, 3, 15, 6
Find the lowest value from this list of values.
How did you come up with your answer?
Set lowest to the first in the list
Go through the list one element at a time
If the current element is smaller than max
replace max with the value of the current element
Output max
Min Program
Adapt your max program so it finds the lowest value in the list.
Update the code comments.
Min Program Solution