3GL Programming Environment
Introduction to Algorithms
|
 |
"An algorithm is a formula, a recipe, a step-by-step procedure
to be followed in order to obtain the solution to a problem.
To be useful as a basis for writing a program, the algorithm
must:
-
arrive at a correct solution within a finite time;
-
be clear, precise and unambiguous;
-
be in a format which lends itself to an elegant implementation in a programming
language."
Peter Juliff, Program Design, 1990
1. Describing an Algorithm
1.1 Flow Charts
Generally uses only four constructs:
1. Start/End specified by a circle,
2. Commands in a rectangle,
3. Decisions in diamonds, with only a Yes or No answer,
4. and flow arrows indicating the next step to follow. Pointing backwards
may indicate a loop.
eg.
Advantages:
-
2-dimensional, so easy to read
Disadvantages:
-
can be difficult to follow
-
flow arrows can go anywhere, which can make converting to program code
difficult.
-
Start is not necessarily positioned at the top and End is not necessarily
at the bottom.
1.2 Nassi-Schneiderman diagrams
Similar to data flow diagrams, except statements are structured so flow
of control is downward. Statements are drawn directly under each other
without the use of arrows to show the flow of control.
eg.
Uses only 3 constructs:
1. sequential statements placed directly under each other
2. iteration (or looping)
3. selection (or decision)
Start is at the top, End is at the bottom. Flow of control is downwards.
Advantages:
-
easy to read and follow
-
top-down. ie.starts at top and ends at the bottom
-
forces an elegant solution to the problem, with construction similar to
a computer program
Disadvantages:
-
Difficult to draw (particularly with a word processor)
-
Even more difficult to edit
1.3 Pseudocode (structured English)
Statements are written in free-flowing English text, although some key
words are defined to represent control structures.
Flow of control is top-to-bottom.
eg.
Calculate student grades
START with students' marks
WHILE another student
Read student's
mark as imark
IF
imark < 50
sgrade = "N"
ELSEIF
imark < 60
sgrade = "D"
ELSEIF
imark < 70
sgrade = "C"
ELSE
sgrade = "B"
ENDIF
PRINT
sgrade
as the student's grade
ENDWHILE
END
Advantages:
-
statements may be as long as needed to describe the particular operation.
-
Many computer languages, such as BASIC, C, C++, Pascal have a syntax which
is similar to Pseudocode therefore are (fairly) easily translated into
program code.
-
do not have to worry about syntax like when writing the algorithm
-
can state something which may take a number of statements in actual code
-
easy to write and edit using a word processor or text editor.
Disadvantages:
-
one-dimensional, so harder to read.
Correct use of indentations can help overcome
this.
-
no real standards.