Identify Ways of Representing Algorithms
Identify Ways of Representing Algorithms
OBJECTIVES:
Identify ways of representing algorithms:
Content
Representation of algorithms as Pseudocode
or Flowcharts; use of flow chart symbols:
input/output process,decision,directional
arrows. Use of: Read, Input, Store, Write,
Output, Display, If Then; If Then Else; For
loop; While loop (Questions which require
nested conditionals or nested loops will not
be asked)
THE ALGORITHMIC LANGUAGE
During the development of an algorithm,
the language gradually progresses from
English towards a notation that resembles
that of a programming language. An
intermediate notation called pseudocode
is commonly used to express algorithms.
Algorithms can also be expressed as
flowcharts.
A flowchart is a pictorial representation of
an algorithm.
Pseudocode
Acceptable words for Input/Output in
pseudocode:
INPUT OUTPUT
Get Display
Accept Print
Input Output
Read
Flowcharts
Flowcharts use special geometrical objects to
designate steps of a program, which are : input,
processing and output. A parallelogram is used
to represent the input operation as well as the
output operation, and a rectangle is used to
represent a processing/assignment statement.
A diamond is used to represent a decision
(If Then Else) structure . An elliptical shape is
used to represent the terminal indicators,
START OR STOP. Directional arrows is used to
indicate the flow of the logic in the algorithm.
Flowcharts Basic Shapes
The four basic shapes are:
Input/Output Processing/Assignment
Decision Start/Stop
Sequence
Below are examples of how the various control
structures are depicted in a flowchart:
Sequence
A
Do A
B
Do B
Selection (The If Then Else Construct)
If C is true then
No Yes
C do E
Else
do D
D E
Loop
Loop (Repetition)
Yes
F G
No
While F is true
do G
Pseudocode Version of the Average Algorithm
Read Num ,
Num ,
Num
Print Aver
Stop
Problem 10
OvertimeHours= HoursWorked - 40
Yes Overtime No
Wages = BasicPay + Wages = HoursWorked * Rate
(OvertimeHours* 2 * Rate) Hours> 0
Print Wages
Stop
Comparison/Selection/Decision Statements
It sometimes becomes necessary when processing data to
compare the value of a variable to the value of another variable
or to compare the value of a variable to that of a constant. The
following Relational Operators are used to make such
comparisons:
Relational Operator Meaning
= Equal to
<> Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
Comparison/Selection/Decision Statements
Examples:
Boys in Class > = 35
NumberOfBoys <> NumberOfGirls
Boolean Operators
When selection or decisions is based upon one or
more expressions being True or False, it is
possible to combine the expressions together
using Boolean Operators AND or OR.
Algorithm AreaTriangle
This algorithm accepts values for the Base and
Height of a triangle and Prints the area.
Print Area
Stop
Question 2
Algorithm Double
This algorithm accepts a number and doubles it and
outputs the result.
Read Num
Double = Num * 2
Print Double
Stop.
Flowchart Version
Start
Read Num
Double = Num * 2
Print Double
Stop
Question 3
Read Age
No Yes
If (Age < 18) Then
Stop
Question 4
Read N
No Yes
If (N > 50) Then
N=N*2
N = N - 10
N=N+5
Print N
Stop