Structure of An Algorithm
Structure of An Algorithm
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’.
4. In algorithm, the arrow () replaces the equal sign (=) when initializing
variables and assigning values into variables.
Types of Algorithms
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.
Problem:
Write an algorithm in narrative form to accept three numbers from the
keyboard, calculate and display their total.
Solution
Problem
Write an algorithm in pseudocode form to accept three numbers from the
keyboard, calculate and display their total.
Solution
1. Set num1 0
Set num2 0
Initialization
Set num3 0 of Variables
Set total 0 The Body
Problem
Draw a flowchart that will accept three numbers from the keyboard, calculate and
display their total.
Solution
Beginning
START
statement
num10
Initialization of
num20 Variables
num30
The Body
total 0
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;
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
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
number1
YES NO
>
number2
Endif
STOP