Algorithm
An algorithm is a set precise of step-by step process in how to solve a problem.
OR
An algorithm is a sequence of precise step-by-step instructions which results in a
solution.
Attributes of an algorithm:
Algorithms have four (4) very important attributes:
1. It must be precise.
2. It must be unambiguous.
3. It must be finite
4. The instructions must be in a logical sequence
Precise -The instructions or steps must be accurate since the computer cannot think
for itself.
Unambiguous -The steps must be very clear so they can be easily carried out.
Finite - The computer must have a definite number of instructions to follow and it
must come to an end.
Logical Sequence - The steps involved in the process must be related to each and
must have a clear flow from one to another.
Structure of an Algorithm
The Header: basically stores the name/title of the Algorithm
The Declaration: a brief description of the algorithm; and the variables and
constants to be used in the algorithm
The Body: the sequence of steps including the beginning Statement
and initialization of variables
The Terminator: the ending statement
NOTE:
1. The Algorithm header must always start with the word Algorithm followed by a
colon i.e. ‘Algorithm:’.
2. The body of the algorithm must always start with the beginning statement i.e.
‘START’.
3. The Terminator must use the keyword ‘STOP’
4. In algorithm, the arrow () replaces the equal sign (=) when initializing
variables and assigning values into variables.
Types of Algorithms
Algorithms can be represented in three (3) ways:
1. Narrative – this is where the instructions of the algorithms are written in
everyday language (English Language)
2. Pseudocode - this is where the instructions of the algorithm is written using
words and symbols that closely resemble a computer programming language
instructions.
3. Flowcharts – this is where the diagrams/shapes are used to represent the
instructions of the algorithm.
NOTE:
The following keywords are used in a Pseudocode Algorithm:
Store, set: - used in an assignment or initialization statement
If-Then-Else-Endif: - used for selection (making comparison)
While-Endwhile; Repeat-Until; For-Do: - used for iteration, repetition or loops
Flow Chart Symbols
used to indicate the beginning/ending or start/stop of a
problem
Begin/Terminator Symbol
(Oval)
Used to indicate
processing (assignment, calculations, initialization of
variables, etc.)
Processing Symbol
(Rectangle)
Used to indicate the input and output of the problem
Input/output Symbol
(Parallelogram)
used in making a decision between two options (yes / no,
true / false)
Decision Symbol
(Rhombus)
Used to show the flow of control of steps.
Flow Control Symbol
(Arrow)
Used to connect sections of a flowchart when a flowchart is
too long and cannot fit on a page. A letter or digit can be
placed in the small circle to indicate the link on the first
Connector Symbol page. Another identically labeled connector is placed on
(Circle) the next page to indicate the continuation of flow. So, two
connectors with identical labels will serve the purpose of a
long line of flow.
Examples of types of Algorithm
Narrative Algorithm Example 1
Problem:
Write an algorithm in narrative form to accept three numbers from the
keyboard, calculate and display their total.
Solution
Algorithm: Total_of_3_numbers Header
This algorithm will accept three numbers, calculate Declaration
and display their totals.
START Beginning statement
1. Get the three numbers. Input Statement
The Body
2. Calculate the total of the three numbers. Processing
Statement
3. Display the total of the three numbers Output
Statement
STOP Terminator Statement
Pseudocode Algorithm Example 1
Problem
Write an algorithm in pseudocode form to accept three numbers from the
keyboard, calculate and display their total.
Solution
Algorithm: Total_of_3_numbers Header
This algorithm will accept three numbers, calculate
and display their totals. The variable that will be used Declaration
are: num1, num2, num3 and total
START Beginning statement
1. Set num1 0
Set num2 0
Initialization
Set num3 0 of Variables
Set total 0 The Body
2. Get num1, num2, num3 Input Statement
3. Set total num1+ num2 + num3 Assignment
Statement
4. Display total Output Statement
STOP Terminator Statement
Flowchart Algorithm Example 1
Problem
Draw a flowchart that will accept three numbers from the keyboard, calculate and
display their total.
Solution
Algorithm: Total_of_3_numbers Header
This algorithm will accept three numbers, calculate
and display their totals. The variable that will be Declaration
used are: num1, num2, num3 and total.
Beginning
START
statement
num10
Initialization of
num20 Variables
num30
The Body
total 0
Get num1, num2, num3 Input Statement
total num1 + num2 + num3 Assignment
Statement
Display total Output
Statement
STOP Terminator
Statement
Narrative algorithm example 2
Problem
Write an algorithm using narrative form to accept two numbers, determine and
print the larger of the two numbers.
Solution
Algorithm: Maximum_number
This algorithm will accept two numbers, determine and print the larger of the two
numbers.
START
1. Accept two numbers
2. Check to see if the first number greater than the second number. If it is,
then;
3. output the first number
4. Otherwise, output the second number.
STOP
Pseudocode algorithm example 2
Problem
Write an algorithm using Pseudocode form to accept two numbers, determine and
print the larger of the two numbers.
Solution
Algorithm: Maximum_number
This algorithm will accept two numbers, determine and print the larger of the two
numbers. The variables that will be used are: number1 and number2.
START
1. Set number1 0
Set number2 0
2. Input number1, number2
3. If (number1 > number2) then
4. Output number1
5. Else
6. Output number2
7. Endif
STOP
Flowchart Algorithm Example 2
Problem
Draw a flowchart that will accept two numbers, determine and print the larger of
the two numbers.
Solution
Algorithm: Maximum_number
This algorithm will accept two numbers, determine and print the larger of the two
numbers. The variables that will be used are: number1 and number2.
START
Number10
Number20
Input number1, number2
number1
YES NO
>
number2
Output number1 Output number2
Endif
STOP