Flowcharts and algorithms
Flowcharts and algorithms
There are two commonly used tools to help to document program logic (the
algorithm). These are flowcharts and Pseudocode. We will use both methods
here. Generally, flowcharts work well for small problems but Pseudocode is used
for larger problems. Some of the common symbols used in flowcharts are shown
below:
With flowcharting, essential steps of an algorithm are shown using the shapes
above. The flow of data between steps is indicated by arrows, or flowlines. For
example, a flowchart (and equivalent Pseudocode) to compute the interest on a
loan is shown below:
Flowchart Pseudocode
Unity University
Note that the Pseudocode also describes the essential steps to be taken, but
without the graphical enhancements. Another example of a flowchart and
the equivalent Pseudocode is shown below. In this case, the program
computes the sum, average and product of three numbers:
Flowchart Pseudocode
Read X, Y,
Z
Compute Sum (S) as X + Y
as S / 3
The example below shows the flowchart for a program that reads two numbers
and displays the numbers read in decreasing order:
Unity University
Read A, B
If A is less than B
BIG = B
SMALL = A
else
BIG = A
SMALL =
B
Write (Display) BIG, SMALL
Unity University
Loops
Most programs involve repeating a series of instructions over and over until
some event occurs. For example, if we wish to read ten numbers and compute
the average, we need a loop to count the number of numbers we have read.
Count loops are loops where the program must count the number of
times operations are completed. The flowchart below illustrates a loop
that counts from 1 to 10:
Count loop flowchart
The flowchart shown on
the left may be simplified
to the form shown on the
right.
While count loops work the exact number of times needed, in many cases we
do not know how many times we want to do something. It is often dependent
on the data provided to the program. Imagine we change our problem to read
and compute the average of a number of numbers. We won't know how many
numbers there are but will read numbers until there are no more.
Unity University
Both these assume that the computer will tell the program when there is no
more numbers. This is called an end-of-data or end-of-file test.
There is an important difference between the pre-test and post-test loops. The pre-
test version
will work even if there are no numbers, the post-test version assumes the body of
the code will be obeyed at least one time. Both forms of loops are appropriate in
different circumstances.
The looping and switching logic above follow well defined rules. In fact, we can
implement any of these constructs with a condition and a goto (unconditional
branch) instruction. An example of this logic is was illustrated in the loop
flowchart shown previously. Early programs were written this way. As the
problems became more complex it became impossible to follow the logic when
things go wrong.
excruciating. However, it is always good fun when you find eventually the error!.
You notice that the logic is nested. Within a loop we may have switching logic
and within that switching logic we may again have a loop, and so it goes.
Program algorithms are usually hierarchical in nature. The style of
programming we are using is called procedural, because we define the solution
in the firm of a procedure indicating what to do. Most problems require this
approach. There are programming methods (or paradigms) where you express
the problem algebraically, or as formal logic. A compiler like program will then
solve the problem for you. A skilled programmer will use the appropriate
method for the nature of the problem being solved.
Additional Reference:
Schaum's Outline of Theory and Problems of Essential Computer
Mathematics, by Dr. Seymour Lipshutz, McGraw-Hill, Inc., ISBN: 0-07-
037990-4. Chapter 5 - Algorithms, Flowcharts, Pseudocode programs.