|
|
STRUCTURED ALGORITHM CHARTS
In the1960's it was mathematically proven that any
programmable problem can be solved using only three logic
control structures, sequence, selection, and repetition (iteration or
recursion), that govern the flow
of execution of any algorithm or program. These along with
modularization structures (procedures
or functions) for "tasks" became the constructs
(building
blocks) of "structured programming", a strategy, developed in
the 1970s, that facilitated the
development of modular "programs" that were easy to develop, debug, and
modify. Previous programming languages, that did not have these
structured programming constructs, relied on
"branch" instructions and the
infamous GOTO instruction to
control the flow of program execution. The GOTO instruction is a
low level (equivalent to machine language) construct, that could jump
to any specified line in a program). This was a powerful
construct, but it allowed (and some say encouraged) undisciplined
software development that promoted "buggy" software. (Note
that the branch instruction is a selection construct of low-level
(machine rather than human oriented) languages, i.e. not a structured
programming construct.)
Flowcharts,
language-independent diagrams developed to illustrate the logic of
algorithms, were oriented towards branch-based
GOTO programming; the arrows of the
flowcharts represented the GOTO statements. Structured
programming, on the other hand, is "GOTO-less
programming" based on structured control constructs (selection and
repetition) rather than branch constructs. Consequently, during the rise of the
structured
programing paradigm, flowcharts fell out of fashion; they were replaced by
pseudocode
(concise, English-like statements that indicated the flow of the
program
logic). However, "A picture IS worth a
thousand words.", so the
methodology
of flowcharting was adapted to the format of structured
programming.
Several versions of structured flowcharting appeared, but,
unfortunately,
none became a standard. The structured flowcharting proposed
by
G. W. Williams, the Editor of Byte
Magazine,
in the March, 1981 issue (no online archive) most effectively
illustrates
the logic and format of structured program code. Structured Algorithm Charts (SACs),
presented
below, are extensions of Williams ideas. A comparison of
the
results of representing an algorithm using old-fashoned flowcharts to a
representation
using SACs is given here.
![]()
![]()
![]()
Note that the SAC examples below (as well as in "my"
references) are all "structured programming" examples that need
to be modified if transferred to an OO environment; in
particular, I/O would not be part of the operation/method algorithms in
an OO architecture!
SAQ1: What is the (a) similarity and (b) difference between a "construct" and a "control structure"?
The goals of this learning module are:
In order to be a completely general tool for developing or illlustrating structured algorithms that are clear, precise, and concise, a SAC must emphasize human-oriented logic (distinct selection and repetition constructs) rather than machine-oriented logic (branch construct used for both selection and repetition) of old-fashioned flowcharts. The following rules reflect this principle. (See Table SAC-1 for a reference to the symbols mentioned.)
2. RULES FOR CONSTRUCTING A SAC:
Modules may refer to separate "subSACs" located
elsewhere, which show the details of the module referenced by the name
written within the symbol. One should distinguish between modules
which have separate subSacs (refresented by solid sided symbols,
e.g.
) and those which are purely descriptive, that is they do not have
separate subSACs (e.g.
). If a language provides modules (methods, functions, or
procedures)
that allow parameter passing, these symbols represent "calls" to the
subprogram named in the parallelogram or rectangle (e.g.
calls a subSAC method that switches the values of
parameters A and
B).
(
Note: the following applies to structured programming, NOT object
oriented software development, i.e. it does not represent the fact
that I/O operations
would be encapsulated in a separate class from processing operations,
i.e.
that I/O and processing are distinct constructs and therefore belong in
distinct
classes.)
|
4. SAC REPRESENTATION OF SELECTION CONSTRUCTS:
This section illustrates the format and use of SAC selection constructs by discussing the simplest possible examples of fundamental selection categories. It reviews the main concepts in selection control structures and the basic techniques used to employ selection correctly and efficiently. The presentation is via example, the classification of a grade into three categories. Note that the classification into three categories is the simplest selection task that still illustrates all the main features of selection. Also note that the following is completely language independent.THE TASK: Design an algorithm component (portion of a SAC) that, depending on the value of the score, S, classifies the grade, G, into three categories: G is "High" if S = 9 or 10, "Medium" if S = 6-8, or "Low" if S =0-5.
4.1 Nested If Implementation of THE TASK (the most efficient way to classify three mutually exclusive items):SAQ 5: Redesign the preceding SAC to select in the following order: "Low", "Medium", and "High". (b) Do these two SACs represent different algorithms?
- Fill in the constructs, below, to make the control structure, categorize the grade G as shown.
![]()
![]()
The real advantage of the SAC approach to algorithm development is that leaving the construct specification details blank allows us to think our way through the logic of a task, without getting "bogged down" with the details. In this case, specifiying the categores while leaving the selection conditions blank helps quickly generate the most efficient selection conditions necessary to fulfill the task. Note that the SAC actually guides us to the solution. This graphically illustrates that the SAC technique is a "power tool" for logical thinking and a key to algorithm development.
4.2 Switch (case) Implementation of THE TASK (three mutually exclusive categories):SAQ 9: (a) Modify the switch implementation of the TASK to include an error trap equivalent to that for the nested if-then-else implementation. (b) Which form of error trap is the most efficient?
- The following selection symbol represents a switch (languages using ANSI standard C terminology) or case ( Pascal-like languages, e.g. Modula and Ada). Fill in the case construct and label the branches so that this control structure is equivalent to that in section 3.1, above.
![]()
4.3 Sequential Implementation of THE TASK (three mutually exclusive categories -SAQ 11: Why, for classification of mutually exclusive items, is the sequential if implementation necessarily less efficient than the nested if implementation?inefficient
):
- Fill in the blanks in the following SAC to make the logic equivalent to the previous implementations of THE TASK.
![]()
- An important question now arises: Is the sequential if implementation of classification as "good" as the nested if implementation? To answer this, consider two cases.
- Assume that your if statements in the sequential implementation were the same as those in the nested if implementation, i.e. the first if had s >= 8 as its condition and the second had the condition S >= 6.
- Do a trace for a complete set of test data, e.g. for values of 9, 7, and 3.
- Hopefully you detected the logic error characteristic of such sequential selections. Note that the logic works for two of the ghree values but not the third. (
This should emphasize the importance of doing a trace of any logic construct with a complete set of test data!) thus the sequential if implementation of mutually exclusive classification is not only more inefficient than the nested if implementation, it is more dangerous because it is easy to overlook subtle bugs!
- The sequential if algorithm can be made to work. If you haven't alreay done so, change the conditions to make the logic correct. (
Hint: use compound conditions.)
- Even if the sequential if implementation does work it is essentially less efficient that the nested if implementation for mutually exclusive categories like those in this example.
5. SAC REPRESENTATION OF REPETITION CONSTRUCTS:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
SAQ 14: What is the (a) similarity and (b) difference
between
(1) iteration and recursion, (2) loop, while loop, and repeat
loop,
and (3) indefinite loop and definite loop?
SAQ 15: A repeat loop can be simulated using a while
loop.
How?
SAQ 16: What looping cases can not be done using a
while
or repeat loop?
6. SAC REPRESENTATION OF CLASSIC ALGORITHMS
(
Note: the following algorithms represent a structured programming
paradigm, NOT object oriented software development, i.e. they do
not represent the
fact that I/O operations would be encapsulated in a separate class from
processing
operations, i.e. that I/O and processing are distinct constructs and
therefore
belong in distinct classes.)