ALGORITHMS
The
Concept of an Algorithm
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Algorithm - informal
definition: |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
A set of steps that
defines how a task is performed. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) | An algorithm is abstract and distinct from its
representation. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
A program is a formal
representation of an algorithm designed for computer applications. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
A process is the activity
of executing the program, or equivalently, the activity of executing an
algorithm. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Parallel algorithms
contain more than one sequence of steps, each designed to be executed by
different processors in a multiprocessor machine. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Algorithm - formal definition: |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
An algorithm is an ordered
set of unambiguous, executable steps, defining a terminating process. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Although, in general, an algorithm must lead to an end
there are applications in real life for non-terminating processes, for example: |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
monitoring the vital
signs of a patient |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) | maintaining an aircraft's in-flight altitude |
Algorithm
Representation
Primitives
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
The representation of
an algorithm requires some form of language. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Problems encountered: |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Terminology used may
have more than one meaning. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Misunderstandings over
the level of detail required. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Primitives are building
blocks for algorithmic representations. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Remove problems of ambiguity
by: |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
Assignment of precise
definitions. |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
Establishing a uniform
level of detail |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Programming Language:
A collection of primitives along with a collection of rules stating how
the primitives can be combined to represent more complex ideas. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
2 Parts of a primitive: |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Syntax - the primitive's
symbolic representation |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Semantics - the concept
represented, or the meaning of the primitive |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Formal Programming Languages:
use a collection of "higher level" primitives, each being an abstract tool
constructed form the lower-level primitives provided in the machine's language. |
Pseudocode
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
A pseudocode is a notational
system in which ideas can be expressed informally during the algorithm
development process. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
The text's approach
to pseudocode is to develop a consistent, concise notation for representing
recurring semantic structures. Some of these structures are: |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
if (condition)
then
(activity) |
else (activity)
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
if (condition)
then
(activity) |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
while (condition)
do
(activity) |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
assign name
the value expression |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
procedure name |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
When a task performed
by a procedure is needed elsewhere, we only have to request it by name. |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
Procedures should be
as generic as possible. |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
The data that the procedure
performs a task on is also assigned a generic name and is listed in parentheses
on the same line as the procedure name. |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
example: procedure
Sort (List) |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Indentation: often enhances
the readability of a program, so it is also used in pseudocode. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Pseudocode provides
a rough outline of an algorithm, not a finished, formal program.
|
Algorithm
Discovery
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
The development of a
program consists of two activities: |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
discovering the underlying
algorithm |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
representing that algorithm
as a program |
The Theory of problem
Solving
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
There is a close association
between the process of algorithm discovery and that of general problem
solving. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Problem Solving Phases
(by mathematician C. Polya): |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Understand the problem. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Devise a plan for solving
the problem. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Carry out the plan. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Evaluate the solution
for accuracy and for its potential as a tool for solving other problems. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Phases of program development: |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Understand the problem. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Get an idea as to how
an algorithmic procedure might solve the problem. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Formulate the algorithm
and represent it as a program. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Evaluate the program
for accuracy and for its potential as a tool for solving other problems. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Polya's 4 phases are
not necessarily completed in sequence. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
The period between conscious
work on a problem and a sudden inspiration is known as an incubation period. |
Getting
a Foot in the Door
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Several general approaches
to going about getting a foot in the door. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Try working the problem
backwards. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Look for a related
problem that is either easier to solve or has been solved before and
then trying to apply its solution to the current problem. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Apply stepwise refinement
-
breaking the original problem into subproblems. |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
Stepwise refinement
is a top-down methodology in that it progresses from the general
to the specific. |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
Solutions produced by
stepwise refinement possess a natural modular structure. |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
If an algorithm has
a natural modular structure, it is easily adapted to a modular representation,
which is conducive to the development of a manageable program. |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
Modules produced are
compatible with the concept of team programming. |
Iterative
Structures
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Iterative structures
--
a collection of instructions is repeated in a looping manner. |
The
Sequential Search Algorithm
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Problem stated: To search
a list for the occurrence of a particular target value. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
We assume the list is
in alphabetical order or in order of increasing magnitude. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Solution: To continue
searching down the list as long as there are more names to be investigated
and the target name is less than the name currently being considered. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
The Sequential Search
algorithm in Pseudocode: |
procedure Search (List, TargetValue)
if (List
empty)
then
(Declare search a failure)
else
[Select the first entry in List as the test entry.
while
(TargetValue > test entry and there remain entries to be
considered)
do (Select the next entry in List as the test entry.);
if (TargetValue = test entry)
then (Declare search a success.)
else (Declare search a failure.)]
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
In the case of long
lists, sequential searches are not as efficient as other techniques. |
Loop
Control
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Loop is an iterative
structure for implementing the repetitive use of an instruction or sequence
of instructions. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
The control of the loop
is the more error prone part of the structure. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Loop control consists
of three activities: |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
initialize -
Establish an initial state that will be modified toward the termination
condition. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
test -
Compare the current state to the termination condition and terminate the
repetition if |
equal.
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
modify -
Change the state in such a way that it moves toward the termination condition. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
There are 2 popular
loop structures that differ only in the order in which the loop control
components are executed: |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
while (condition)
do
(activity) |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
test for termination
occurs before the loop's body is executed. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
repeat (activity)
until
(condition) |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
test occurs after
the loop's body is executed |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
loop's body is always
performed at least once. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Flowcharts - use various
shapes to represent individual steps and arrows to indicate the sequence
of steps. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Diamond indicates a
decision. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Rectangle indicates
an arbitrary statement or sequence of statements. |
The
Insertion Sort Algorithm
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Goal: to sort
the list within itself. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
This is a typical restriction
because we want to use the storage space available in an efficient manner. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Generalize a process
to obtain an algorithm: |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Pick the first name
in the unsorted portion of the list, slide the names greater than the extracted
name down, and insert the extracted name back in the list where the hole
appears. |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
Identify the extracted
name as the pivot entry. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Note that the process
should be executed repeatedly, so we need to use an iterative structure. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Formulate the pseudocode
for the process: |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Move the pivot entry
to a temporary location leaving a hole in List; |
while (there is a name above the hole and that name is greater
than the pivot) do
(move the name above the hole down into the hole leaving a hole above the
name)
Move the pivot entry into the hole in List.
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
The insertion sort
expressed
in pseudocode: |
procedure Sort (List)
if (there are two or more entries in List) then
[Shade the portion of List from the second entry through the last entry;
repeat
(Remove the shading from the first name in the shaded portion of List
and identify this name as the pivot entry;
Move the pivot entry to a temporary location leaving a hole in List;
while (there is a name above the hole and that name is greater than
the pivot) do
(Move the name above the hole down into the hole leaving a hole above
the name)
Move the pivot entry into the hole in List.)
until (entire List is unshaded)]
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
The components of repetitive
control in Insertion Sort: |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Outer Loop: |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
initialize -- shading
the portion of List from the second entry to the last |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
test -- when shaded
portion of list becomes empty |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
modify -- Remove shading
from first name and identify as pivot entry |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Inner Loop: |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
initialize -- remove
pivot entry from List and create a hole |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
test -- hole is below
a name that is not greater than the pivot or hole reaches top of
List. |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
modify -- Moving entries
above the hole down, thus moving the hole up. |
Recursive
Structures
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Recursive structures
provide an alternative to the loop paradigm for repetitive structures. |
The
Binary Search Algorithm
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Binary Search Algorithm
applies
a divide-and-conquer methodology to the search process. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Divides the list in
half, then in half again, etc. until the Target Value is found. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Assumes a generic sorted
list. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Apply the same Search
technique to the smaller lists that we apply to the original one. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Progress in the original
copy of the list is temporarily suspended while we apply the second copy
to the task of searching the smaller list. It reports its findings
to the original copy and progress is continued in the original. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
The Binary Search Algorithm
in Pseudocode: |
procedure Search (List, TargetValue)
if (list empty)
then
(Declare the search a failure)
else
[Select the middle entry in List as the test entry;
Execute one of the following blocks of instructions depending on whether
TargetValue is
equal to, less than, or greater than the test entry.
TargetValue = test entry;
(Declare the search a success.)
TargetValue < test entry;
[Apply the procedure Search to see whether TargetValue is in the portion
of List
preceding the test entry, and
if (that search is successful)
then (Declare this search a success.)
else (Declare this search a failure.)]
TargetValue > test entry;
[Apply the procedure Search to see whether TargetValue is in the portion
of List
following the test entry, and
if (that search is successful)
then (Declare this search a success.)
else (Declare this search a failure.)]]
Recursive
Control
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Recursion - Each stage of the repetition is
executed as a subtask of the previous stage. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
The execution of a recursive algorithm creates
multiple copies of the algorithm called activations. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Just as in loop control, recursive systems
are dependent on testing for a termination condition and on a design that
assures this condition will be reached. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Testing for the condition is done before
requesting further activations. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
All activations that are generated ultimately
terminate, leaving the original task completed. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Equivalence of iterative and recursive structures
is proven in Appendix E. |
The
Quick Sort Algorithm
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Goal of Quick Sort Algorithm
- to sort a list within itself. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Approach: Select
a name, called the pivot entry, find its correct position, and place the
name in that position. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Choose the top entry
as the pivot. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
There is a pointer to
the pivot and to the last name. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
The algorithm directs
the movement of the pointers toward each other in such a way that the following
assertion is always satisfied: |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Assertion 1:
The names above the top pointer are less than (alphabetically) or equal
to the pivot entry, and those below the bottom pointer are greater than
the pivot. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
When the two pointers
coincide, we have found the correct location for the pivot. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
We interchange the pivot
with the entry identified by the common pointers. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
As the bottom pointer
is moved up the list, as long as the name is greater than the pivot, the
pointer is moved up. When the name is less than or equal to the pivot entry,
the movement of the pointer is stopped. The top pointer is
moved down the list as long as the name compared to the pivot is less than
or equal to the pivot. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
If the pointers do not
coincide, a deadlock is produced, which we break by interchanging the names
designated by the pointers. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Assertion 2:
At the beginning of the sort process and after each interchange for breaking
a deadlock, the top pointer points to a name less than or equal to the
pivot. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Assertion 3:
If the pointers coincide, the entry they identify is less than or equal
to the pivot. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
The list is now 2 lists:
the list above the pivot and the list below it. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
We consistently apply
the algorithm to each list, which in turn create smaller and smaller lists,
until the lists contain no more than one name. When no more applications
of the process are required, the list is sorted. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Pseudocode for the Quick
Sort Algorithm: |
procedure Sort(List)
if (List contains fewer than two entries)
then (Declare List to be sorted)
else
[Select the first entry in List as the pivot:
Place pointers at the first and last entries of List;
while the pointers do not coincide) do
(Move the bottom pointer up to the nearest entry less
than or equal to the pivot but not beyond the top
pointer;
Move the top pointer down to the nearest entry greater
than the pivot but not beyond the bottom pointer;
if (the pointers do not coincide)
then (interchange the entries indicated by
the pointers.))
Interchange the pivot entry with the entry indicated by
the common pointers;
Apply the procedure Sort to the portion of List above
the pivot.
Apply the procedure Sort to the portion of List below
the pivot.
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
The last two statements
of the pseudocode create further activations of the algorithm, which is
recursion. |
Efficiency
and Correctness
Algorithm
Efficiency
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
The ultimate efficiency
of an algorithm is closely associated with the details of its implementation. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Typically, a major concern
regarding an algorithm's implementation is data organization. |
Software
Verification
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
There is a distinction
between a program that is believed to be correct and a program that is
correct. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
A formal proof of a
program's correctness is based on the specifications under which the program
was designed. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Proof of correctness
begins with the assumption that certain conditions, called preconditions,
are satisfied at the beginning of the program execution. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Proof of correctness
proceeds by identifying statements, called assertions, that can
be established at various points in the program. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
The result is a collection
of assertions, each being a consequence of the program's preconditions
and the sequence of instructions that lead to the point in the program
at which the assertion is established. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
If the assertion so
established at the end of the program corresponds to the desired output
specifications, we can conclude that the program is correct. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
In the structure of
a loop, a certain condition is true each time the loop is executed.
Such an assertion within a loop is called a loop invariant. |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
Example: In
the Insertion Sort, the loop invariant is stated as: |
Each time the loop body is completed the names in the unshaded portion
of the
list are sorted.
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Assertions obtained
when proving a program correct are often essentially the insights that
led tot he program in the first place. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
In most cases today,
software is "verified" by applying it to test data, which only proves that
the program runs correctly for the test data. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Test data should not
be designed by the person(s) writing the program. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Languages have been
developed that allow humans to avoid the intricacies of registers, memory
addresses, and machine cycles during the program development process and,
instead, to concentrate on the properties of the problem being solved. |
Historical
Perspective
Early
Generations
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Originally all algorithms
were expressed in the machine's language. -- first generation language |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Later, mnemonics were
assigned to the various op-codes and used in place of hexadecimal representation. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Example: names R0, R1,
etc. were assigned to the registers in the CPU. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
This use of mnemonics
was formalized into a programming language, assembly language. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Second generation
language |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Assembler ==
a program to translate assembly language into machine language. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Machine dependent |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Programmer still had
to think in small, incremental steps instead of concentrating on the solution
to the problem. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Not easily transported
to another machine. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Third Generation
Languages - use collections of high level primitives to develop software. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Each high level primitive
is implemented as a collection of lower level primitives used by the machine. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Translator - a
program to translate high level primitives into machine language. |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
Translator is now called
a compiler. |
Machine
Independence and Beyond
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Since statements in
a 3gl do not refer to the attributes of any particular machine, they are
compiled as easily for one machine as for another. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
However, some differences
among machines, such as the size of registers and memory cells still affect
portability. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
ANSI and IOS (American
National Standards Institute and the International Organization for Standardization)
have adopted and published standards for many of the popular languages. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
3gl's are close enough
to being machine independent that software can be transported from one
machine to another with relative ease. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Fourth generation
language is used in reference to software packages that allow users
to customize computer software to their applications without needing technical
expertise. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Usually involves a selection
from choices presented on the monitor's screen in sentence or icon form. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Packages include spreadsheet
systems, database systems, and graphics packages, and powerful word processors. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Packages are often bundled
(as integrated software) to form one coherent system. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Fifth generation
language refers to declarative programming, with an emphasis
on logic programming. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
User concentrates on
the problem's characteristics instead of on how the problem is solved. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
User describes a problem
and the machine solves it. |
Programming
Paradigms
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
A paradigm is an approach
to the programming process. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Four Programming Paradigms: |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Functional
(LISP, ML, Scheme) |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
Process of program development
is the construction of "black boxes", each of which accepts input at the
top and produces output at the bottom. |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
"Boxes" = Functions |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
construction of functions
as nested complexes of simpler functions. |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
Elaborate functions
are constructed from elementary functions (the primitives of the language). |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
Problem is approached
by considering the input that is given, the output that is desired, and
the transformation that is required to produce that output from the given
input. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Object Oriented (SIMULA,
C++, Ada '95, Smalltalk, Java) |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
leads to Object-Oriented
Programming (OOP) |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
Units of data are viewed
as active "objects" |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
Object is the data together
with a collection of routines for manipulating the data. |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
another example ==
GUI (graphical user interface.) |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
Icons are
implemented as objects. |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
Routines describe how
objects should respond to various events (i.e., clicking or dragging). |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
Result is an event-driven
system. |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
examples are Visual
Basic (by Microsoft) and Delphi (by Borland) |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Imperative
(FORTRAN, BASIC, C, COBOL, ALGOL, APL, Ada, Pascal) |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
Also known as procedural
paradigm |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
represents the traditional
approach to programming |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
approach a problem by
trying to find a method for solving it. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Declarative
(GPSS, Prolog) |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
Asks "What is the problem"
instead of how to solve it. |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
Developing a precise
statement of the problem rather than of discovering an algorithm for solving
the problem. |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
Early declarative languages
tended to be special-purpose in nature. |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
Used in simulations
-- task is to describe the relationships among the parameters to be simulated. |
Traditional
Programming Concepts
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Three categories of
statements in programming languages: |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
declarative statements
- define customized terminology that is used later in the program , such
as the names used to reference data items. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
imperative statements
-
describe the steps in the underlying algorithms |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
comments - enhance
readability |
Variables,
Constants, and Literals
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
variables - identification
of memory locations by means of descriptive names. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
value associated with
the identifier changes as the program executes. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
literal - a reference
to a fixed, predetermined value. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Use of literals is not
good programing practice. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
constants - assignment
of descriptive names to specific, nonchangeable values. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
better portability |
Data
Type
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Data Type refers
to both the interpretation given a bit pattern and the operations that
can be performed on that pattern. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Common data types: |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Integer - numeric
data consisting of whole numbers, probably stored in two's complement notation. |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
Operations include arithmetic
and relational |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Real - numeric
data that may contain values other than whole numbers, probably stored
in floating point notation. |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
Operations include arithmetic
and relational |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Character - data
consisting of symbols, probably stored using ASCII. |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
Operations include comparison,
concatenation, and testing to see if one string is inside another. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Boolean - can
only take on the values true or false. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
A declarative statement
that introduces a variable usually must specify the type of data that will
be referenced by that variable. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Information on data
types can be used to identify errors. |
Data
Structure
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Data structure relates
to the conceptual shape of the data. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
A string has
both type and length. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
homogeneous array
-
a block of values of the same type such as a string or an array of one
or more dimensions. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
components are identified
via indices that specify the row, column, and so on. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
heterogeneous array
-
a block of data in which different elements can have different types. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
components are referenced
by identifying the array, followed by the component name separated by a
period. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Once an array has been
declared, it can be referenced in its entirety by name or an individual
component can be referenced by its position. |
Assignment
Statements
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
perhaps the most basic
imperative statement |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
requests that a value
be assigned to a variable. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Example: |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Total = Price + Tax;
(in C and C++) |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Total := Price
+ Tax; (in Ada and Pascal) |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Ambiguities in interpretation
are resolved by the rules of operator precedence, meaning that certain
operations are given precedence over others. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
overloading --
multiple use of a symbol; many languages allow the use of one symbol to
represent more than one type of operation, and the operation performed
depends on the data types involved. |
Control
Statements
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Control statements
are
imperative
statements that alter the execution sequence of the program. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
goto statement
is bad programming practice |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Control statements of
modern languages allow a certain branching pattern to be expressed within
a single syntactic structure. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Structured programming
-
encompasses an organized design methodology combined with the appropriate
use of the language's control statements. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
produces a program that
is easily understood and meets specifications |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Common branching structures: |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
if-then-else |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
while |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
case - allows
a selection between many options |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
for - similar
to while, but all the initialization, modification, and termination are
incorporated into one statement. |
Comments
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Comments are internal
documentation ignored by the compiler or translator. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Comments should be meaningful,
adding clarity rather than length to a program. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
A good approach is to
collect comments that relate to a single program unit into one place, perhaps
at the beginning of the unit. |
Program
Units
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Should group steps in
an algorithm to form short, simple portions of the algorithm and to use
these portions as abstract tools to express the final product. |
Procedures
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
A procedure
is a program unit written independently of the main program yet associated
with it through a transfer/return process. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Transferring control
to a procedure is also called calling or invoking the procedure. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
The program unit that
requests the execution of the procedure is the calling unit. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
The definition begins
with a statement, known as the procedure's header, that identifies,
among other things, the name of the procedure. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
As a general rule, the
variables declared within a procedure are Local variables, meaning
that they can be referenced only within that procedure. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
An item of data shared
by all units within the program is represented by a variable called a global
variable. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
not a recommended technique |
Parameters
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Explicit identification
of data being shared is done by listing the data items in the procedure
call and in the procedure's header. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
The items in both lists
are called parameters. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
In Ada, the keywords
in,
out, and in out are used. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Once a procedure has
been defined, it can be used from within another program unit. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Names used for the parameters
within
the procedure are called formal parameters. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Data values supplied
from
the other program unit are called actual parameters. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
When the amount of data
associated with the parameters being passed is large, a more efficient
method is to transfer only the address of the memory cells
that contain the data. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
This is called passed
by reference. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
If the information is
actually copied, it is called passed by value. |
Functions
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Function refers
to a program unit similar to a procedure, except that a value is
transferred back to the main program as "the value of the function" rather
than via the parameter list. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Value is associated
with the function's name. |
I/O
Statements
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Implementations of I/O
operations: procedures and functions being called are actually routines
within the machine's operating system. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Examples: |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Pascal
-- readln (Value); |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
C
-- printf("%d %d\n", Value1, Value2); |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
C++
-- provides ready made objects
known as cin and cout |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
cin >> Value;
and cout
<< Value; |
Program
Examples
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Sample programs in Pascal
and C are shown in Figure 5.12 on page 210 of the text. |
Language
Implementation
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Process of converting
a program written in a high-level programming language into a machine-executable
form. |
The
Translation Process
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Translation --
the process of converting a program from one language to another |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
source program --
program in its original form |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
object program --
the translated version |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Three activities of
the translation process: |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
lexical analysis-
process of recognizing which strings of symbols from the source program
represent a single entity. |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
generates a bit pattern
known as a token indicating the unit's class (i.e. a value, a word,
etc.) |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
parsing-
process of identifying the grammatical structure of the program and recognizing
the role of each component. |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
fixed-format languages
-- insisted each program statement be positioned in a particular manner
on the printed page. |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
free-format languages
-- the positioning of the statements is not critical. |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
the language's syntax
must be designed so the structure of the program can be identified regardless
of spacing. |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
this is done through
the use of punctuation marks, key words, and reserved words. |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
parsing process is based
on a set of syntax rules |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
syntax diagrams --
pictorial representations of a program's grammatical structure. |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
terms that require further
description -- (in rectangles) -- nonterminals. |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
terminals appear
in ovals. |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
a string can be represented
by a parse tree. |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
Parser analyzes declarative
and imperative statements and ignores the comments |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
symbol table --
record of information from declaration statements |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
implicit conversion
between types is called coercion. |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
most modern day languages
are strongly typed -- all activities must involve data of agreeable
types without coercion. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
code generation-
constructing the machine language instructions to simulate the statements
recognized by the parser. |
![](_themes/sumi-painting-smallfont/sumbul3a.gif) |
The code generator is
responsible for code optimization |
Linking
and Loading
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
The job of the linker
is
to link several object programs, operating system routines, and utility
software to produce a complete, executable program (sometimes called a
load
module. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
To execute a translated
program, the load module must be placed in memory by a program called a
loader. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
Loader
is usually part of the operating system's scheduler. |
![](_themes/sumi-painting-smallfont/sumbul2a.gif) |
A relocatable module
executes
correctly regardless of where it executes in memory. |
In summary, the complete
task of preparing a high-level language program for execution consists
of the three-step sequence of translate, link, and load.
Software
Development Packages
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Grouping of a translator
along with an editor, debugging tools, etc. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
System would be classified
as application software. |
![](_themes/sumi-painting-smallfont/sumbul1a.gif) |
Future programming process
-- might be construction of software from large prefabricated blocks. |
Sorry,
this section hasn't been entered yet, but will be soon, I hope.
Or, if you would like to go to my pages on my Software Engineering Class, click
on Software Engineering above.
![](_themes/sumi-painting-smallfont/sumhorsa.gif)
![](_themes/sumi-painting-smallfont/sumhorsa.gif)
|