How Are Algorithms Developed?

advertisement
How Are Algorithms
Developed?
CPS120:
Introduction to Computer Science
Lecture 6
Algorithms

Muhammad ibn Musa
Al'Khowarizmi, a Tashkent
cleric who in the twelfth
century developed the
concept of a written
process to be followed to
achieve some goal, and
published a book on the
subject that gave it is
modern name -- algorithm.
What is an Algorithm?

An algorithm is merely the sequence of steps
taken to solve a problem

Two parts




Actions to be executed
Order in which those actions are to be done
Computational steps that transform the input data
into useful output data.
Algorithms are not programs

They need to be coded in a programming
language like C++
Programs are Solutions to Problems
 Programmers
arrive at these solutions
by using one or more of these devices:
Logic flowcharts
 Structure charts
 Pseudocode (sue-dough'-code)
 Structured Programming

 Solutions
to problems need to be
developed before code is written
Writing C++ (Source) Code

C++ syntax can obscure understanding



Write out solutions intuitively – on paper
Use flowcharts or pseudocode
Translate ideas to C++ syntax on paper

Try to NEVER compose code at the terminal
 Don't try to crate the perfect solution on the
first try – it's too difficult


Begin with a partial solution that runs
Make it better iteratively through refinement

Prototyping
Design Philosophy
 Spend
a little more on an organized
software development approach to save
a lot during testing, maintenance, and
support phases
Pseudocode & Flowcharts are Important

Pseudocode –


Make a detailed description of your algorithm’s
logic before worrying about C++ syntax and data
layout.
An algorithm you develop using pseudocode
should be capable of implementation in any
procedural programming language


Pseudocode is generally independent of the
implementation language
Flowcharts –

A graphical layout of the algorithm is often very
useful in spotting “illogical” logic!
Flowcharts and Pseudocode
 Forms
of documentation used to build
and communicate the detailed parts
your structured designs
Flowchart
 A graphical
algorithm.
representation of an
Reasons Programmers Draw Flowcharts

Drawing a flowchart gives the programmer a
good visual reference of what the program
will do
 Flowcharts serve as program documentation
 Flowcharts allow a programmer to test
alternative solution to a problem before
coding
 Flowcharts provide a method for easy desk
checking
Logic Flowcharts
 These
represent
the flow of logic in
a program and
help programmers
“see” program
design.
Common Flowchart Symbols
Common Flowchart Symbols
Terminator. Shows the starting and ending points of the program. A terminator has
flow lines in only one direction, either in (a stop node) or out (a start node).
Data Input or Output. Allows the user to input data and results to be displayed.
Processing. Indicates an operation performed by the computer, such as a variable
assignment or mathematical operation. With a heading – an internal subroutine
Decision. The diamond indicates a decision structure. A diamond always has two
flow lines out. One flow lineout is labeled the “yes” branch and the other is labeled the
“no” branch.
Predefined Process. One statement denotes a group of previously defined statements.
Such as a function or a subroutine created externally
Connector. Connectors avoid crossing flow lines, making the flowchart easier to read.
Connectors indicate where flow lines are connected. Connectors come in pairs, one with
a flow line in and the other with a flow line out.
Off-page connector. Even fairly small programs can have flowcharts that extend several
pages. The off-page connector indicates the continuation of the flowchart on another
page. Just like connectors, off-page connectors come in pairs.
Flow line. Flow lines connect the flowchart symbols and show the sequence of operations
during the program execution.
Rules for Drawing Flowcharts
 Top
to bottom and left to right
 Draw
the flowchart the way you like to read
 Use arrowheads on flow lines whenever
the flow is not top to bottom, left to right
 Be
neat ! Use graphics software
 Avoid intersecting lines
Flowchart for a
Cash Register Program
Start
or
Preparation Symbol
sum=
Input price
sum=sum+price
Yes
Terminal Symbol
More
items?
No
tax=sum x 0.0725
total=sum+tax
Output sum, tax,
and total
Stop
I/O Symbol
Process Symbol
Decision Symbol
Disadvantages to Flowcharts
 Time
consuming
 A program flowchart shows how the
input becomes output, but it does not
show why a particular step is done
 Flowcharts are subjective
What is Pseudocode?
 Pseudocode
is an artificial and informal
language that helps programmers
develop algorithms.
 Pseudocode
is a "text-based" detail
(algorithmic) design tool.
 An English description of an algorithm in
sufficient detail to allow its implementation
to be easily written.
Pseudocode
 This
device is not visual but is considered
a “first draft” of the actual program.
 Pseudocode is written in the programmer’s
native language and concentrates on the
logic in a program—not the syntax of a
programming language.
General Rules for Pseudocode
 There
is no standard pseudocode
 The rules of Pseudocode are generally
straightforward
 Should
be easily read and understood by
non-programmers
 All statements showing "dependency" are
to be indented.

These include while, do, for, if, switch
Writing Pseudocode
 You
need to reach a balance between
excessive and insufficient detail.
 In
some cases you only need a skeleton of
the logic and in other cases you may want
to show the intricacies of your algorithm
 Write what is necessary to understand and
communicate the essential parts of your
algorithm without becoming bogged down
in detail
Pseudocode Statement Rules



Statements are written in a simple English-like
language
Each instruction is started on a separate line
Logic-showing keywords are written in UPPER
CASE or typed in BOLD UPPERCASE





(e.g. IF, THEN, FOR, DO etc.)
These are the only uppercase words in this form of
pseudocode.
Indentation is used to show structure
Instructions are written from top to bottom, with
only one entry point and one exit point
Logically related groups of instructions can be
formed into modules and given a name
Pseudocode for a Cash Register Program
sum=0
While More items do
Input price
sum=sum+price
End While
tax=sum x 0.0725
total=sum+tax
Output sum, tax, total
Rules for Pseudocode
1.
2.
3.
4.
5.
6.
7.
8.
Make the pseudocode languageindependent
Indent lines for readability
Make key words stick out by showing them
capitalized, in a different color or a different
font
Punctuation is optional
End every IF with ENDIF
Begin loop with LOOP and end with
ENDLOOP
Show MAINLINE first; all others follow
TERMINAE all routines with an END
instruction
Structured Programming
 Structured
program languages lend
themselves to flowcharts, structure
charts, and pseudocode.
 Structured programming languages
work best where the instructions have
been broken up into small, manageable
parts.
Structure Charts
 Structure
charts illustrate the structure
of a program by showing independent
hierarchical steps.
 Major divisions are subdivided into
smaller pieces of information.
Download