CPS 120: Introduction to Computer Science

Download Report

Transcript CPS 120: Introduction to Computer Science

CPS 120: Introduction to
Computer Science
Algorithm Design
and
Problem Solving
Session Goals
• Determine whether a problem is suitable for
a computer solution
• Describe the computer problem-solving
process
• Distinguish between following an algorithm
and developing one
• Apply top-down design methodology to
develop an algorithm to solve a problem
The Program Development Cycle
Analyze the problem
Design the solution algorithm
Design the user interface
Write the code
Test and debug the program
Complete the documentation
Problem Solving
• Problem solving is the act of finding a
solution to a perplexing, distressing, vexing,
or unsettled question
Problem Solving
• In 1945 George Polya wrote How to Solve
It: A New Aspect of Mathematical Method
• His How to Solve It list is quite general
– Written in the context of solving
mathematical problems
– The list becomes applicable to all
types of problems
Ask Questions...
• …to understand the problem
– What do I know about the problem?
– What is the information that I have to process in order
the find the solution?
– What does the solution look like?
– What sort of special cases exist?
– How will I recognize that I have found
the solution?
Look for Familiar Things
• You should never reinvent the wheel
• In computing, you see certain problems
again and again in different guises
• A good programmer sees a task,
or perhaps part of a task (a subtask),
that has been solved before and plugs
in the solution
Divide and Conquer
• Break up a large problem into smaller units
that we can handle
– Applies the concept of abstraction
– The divide-and-conquer approach can be
applied over and over again until each subtask
is manageable
Computer Problem-Solving
The computer problem-solving process
The Interactions Between ProblemSolving Phases
The Program Development Cycle
Analyze the problem
Design the solution algorithm
Design the user interface
Write the code
Test and debug the program
Complete the documentation
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++, Visual Basic, COBOL, Java, etc.
Algorithms
• An algorithm is set of instructions for
solving a problem or sub-problem in a finite
amount of time using a finite amount
of data
• The instructions are unambiguous
Following an Algorithm
• Preparing a Hollandaise sauce
Following an Algorithm (cont.)
• Preparing a Hollandaise sauce
Programs are Solutions to Problems
• Programmers arrive at these solutions by
using one or more of these devices:
•
•
•
•
Structured Programming
Structure charts
Logic flowcharts
Pseudocode (sue-dough'-code)
• Solutions to problems need to be developed
before code is written
Developing an Algorithm
• The plan must be suitable in a suitable form
• Two methodologies that currently used
– Top-down design
– Object-oriented design
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.
Top-Down Design
• Breaking the problem into a set of subproblems called modules
• Creating a hierarchical structure of
problems and sub-problems
Structure Charts
• Structure charts illustrate the structure of a
program by showing independent
hierarchical steps.
• Major divisions are subdivided into smaller
pieces of information.
Top-Down Design
An example
of top-down
design
• This process continues for as many levels as it takes to expand
every task to the smallest details
• A step that needs to be expanded is an abstract step
A General Example
• Planning a large party
Subdividing the party planning
Flowcharts and Pseudocode
• Forms of documentation used to build and
communicate the detailed parts your
structured designs
Flowchart
• A graphical representation of an algorithm.
Pseudocode
• Uses a mixture of English and formatting to
make the steps in the solution explicit
Flowcharts & Pseudocode are Important
• Flowcharts –
– A graphical layout of the algorithm is often very useful
in spotting “illogical” logic!
• Pseudocode –
– Make a detailed description of your algorithm’s logic
before worrying about 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
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.
How to Draw a Flowchart
•
Five steps which can be used as a guide for
completing flowcharts.
1. Start with a 'trigger' event (it may be the beginning of
the program)
2. Initialize any values that need to be defined at the start
of the program
3. Note each successive action concisely and clearly
4. Go with the main flow (put extra detail in other charts
-- this is the basis of structured programming)
5. Follow the process through to a useful conclusion
(end at a 'target' point -- like having no more records
to process)
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
Sample Program Flowchart
Terminal Symbol
Preparation Symbol
Start
or
I/O Symbol
Initialize Variables
Variables =
Open
Files
Read a
Record
No
Decision Symbol
More
items?
Yes
Process Symbol
Process
Record
(Detail Time)
Write
Record
Process
Record
(Total Time)
Close
Files
Stop
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
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 nonprogrammers
– 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.
– Write only what is necessary to understand and
communicate the essential parts of your
algorithm
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 Generalized Program
START
Intialize variables
LOOP
While More records do
READ record
PROCESS record
PRINT detail record
ENDLOOP
CALCULATE TOTALS
PRINT total record
END
Rules for Pseudocode
1. Make the pseudocode language-independent
2. Indent lines for readability
3. Make keywords stick out by showing them
capitalized, in a different color or a different font
4. Punctuation is optional
5. End every IF with ENDIF
6. Begin loop with LOOP and end with ENDLOOP
7. Show MAINLINE first; all others follow
8. TERMINATE all routines with an END
instruction
A Computer Example
• Problem
– Create an address list that includes each
person’s name, address, telephone number, and
e-mail address
– This list should then be printed in alphabetical
order
– The names to be included in the list are on
scraps of paper and business cards
A Computer Example
A Computer Example
A Computer Example
A Computer Example
A Computer Example
Relation of Flowcharts &
Pseudocode
The Program Development Cycle
Analyze the problem
Design the solution algorithm
Design the user interface
Write the code
Test and debug the program
Complete the documentation
Data Processing
• Facts enter the computer
• They are:
–
–
–
–
Checked for accuracy
Organized
Calculated with
Stored