SAC
EXAMPLE
4
Example of the
Multiple Nesting of Selection Constructs
(
NOTE:
This does not implement the OOSD paradigm; see the last extension.)
The following SAC represents a monolityic (all inclusive) algorithm
for
classifying
a triangle according to whether it is equilateral (all
sides
equal), isosceles (any two sides equal), or scalene (no
sides
equal). This version assumes that the lengths of the three sides
(A, B, and C) are input. Complete the SAC by inserting into
each unfilled output symbol one of the following: "equilateral",
"isoceles",
or "scalene". (NOTE: This algorithm
is
far from perfect; extensions 2 - 5 (following the illustration) show
how
it can be optimized.)

SAQ
1: See EXTENSION 1.
EXTENSIONS:
- How many different (but equivalent) algorithms
can
be made
by altering the conditions? {MAKE INTO AN SAQ}
- Modify the SAC, using compound conditions,
so that
only two selection control structures are needed to accomplish the same
tasks. (See SAQ 2.) (b) How many different, but equivalent
algorithms could you have now?
- What changes, if any, would be necessary to
use
angles
as input data instead of side lengths. (b) Which (angles or
side lengths) is best? Does this depend on anything, or is one
always best?
- Modify the SAC so that it can distinguish
right
triangles
(one angle of 900)
as well.
-

CLASSIFY_TRIANGLE
illustrates a "monolithic
algorithm",
i.e. one long collection of instructions that are not "highly cohesive"
because the I/O and processing are combined in one algorithm. (a)
Redesign
the
SAC so that the I/O algorithm is independent of the Process algorithm. (b) How could
this modularization be implemented with OO constructs?

(This
is the "final exam" for selection algorithms, i.e. it covers everything
necessary to "understand" structured selections!) (a) All of the
preceding algorithms will work for erroneous input
data (sides or angles which do not form triagles). Modify the
algorithm
of Extension 5 so that an "error trap" would give a
warning if
data,
that did not specify a triagle, were input. (b) Could such an
error trap
be incorporated if side lengths were used as data instead of
angles? (c) Consider all
the
different organizations by reorganizing the selection conditions, e.g.
which condition should be tested first? Is any particular
algorithm optimal?
(d)
Consider
classifying triangles into other categores, e.g. according to color,
size, etc. How would these be added to our algorithm?
(e) Do
you get the very important point of the destinction between
if-then-elses in sequence and those that are nested? Do
you see how focusing on this helps "problem solving" in algorithm
development and how SACs become "power tools" for this development
(e.g. how they can be used experiment with different logical
organizations)? If not, review SAC EXAMPLE
3, extension 4.
- (a) Write the code that implements the SAC
of the
optimum algorithm for this operation, in the language of your
course.
(b) What can be said about the code for the
Java,
JavaScript, C++, and C implementations of this algorithm?
- How
would the algorithms in this example be modified to represent OOSD?
(b) Would the different versions of this algorithm have different OOSD
versions?
SAQ
2: Note that the three types of triagles (equilateral, isosceles, and
scalene)
are mutually exclusive. (a)
What is the minimum
number of selection constructs needed to distinguish three mutually
exclusive
categories? (b) Generalize this observation to predict the
minimum
number of selections constructs required to destinguish N mutually
exclusive
categores. (c) How many different, but equivalent, algorithms are
there with the minimum number of selections?
SAQ
3: Why would it be impossible to write polymorphic operations (with the
same name) for angle data and side data, that have the same number of
arguments?
SAQ
4: Which is the most useful type of arguments, side lengths, or
angles?
(Hint: If all you had were the length of the sides, how could you
determine
whether or not a triagle is "right", i.e. has a 900
angle?)