This preview shows page 1. Sign up to view the full content.
Unformatted text preview: Chapter 6: Problem Solving and
Chapter
Algorithm Design
In general, how are problems solved on a
In
computer?
computer? Chapter 6
Problem
Problem
Solving and
Algorithm
Design
Design
Page 1
Page Algorithms
Algorithms
An algorithm is an ordered set of unambiguous, executable
An algorithm
steps that ultimately terminate if followed.
steps
Ambiguou
s: Not
Not
executable:
executable: No
No
termination:
termination: • Lather
• Rinse
• Repeat Chapter 6
Problem
Problem
Solving and
Algorithm
Design
Design
Page 2
Page Computer Algorithms
Computer
In computer programming, an algorithm is the sequence of
In
steps (i.e., the “recipe”) for accomplishing a task.
steps
Every step in an algorithm has two basic components:
1. Semantics: The meaning of the step
2. Syntax: The format of the step Chapter 6
Problem
Problem
Solving and
Algorithm
Design
Design
Page 3
Page Pseudocode
Pseudocode
Pseudocode is an informal notation for expressing an
Pseudocode
Procedure Sat1231A
algorithm.
algorithm. Set year to 2001
year
Example: Which
Which
Set month to January
month
years in 20012020
Set day to first Saturday in January 2001
day
have New Year’s
While (year < 2021) Do
While
{
Eve on Saturday?
Eve
Increment day by 7
Increment
If date is New Year’s Eve
If
Then display year as having a Saturday New Year’s Eve
year
If day > (number of days in month)
day
month
Then
Then
{
Adjust day by subtracting the number of days in
day
month
month
If month is December
month
Then
Then
{
Increment year by 1
year
Set month to January
month
}
Else
Else
Increment month by one
month
} Chapter 6
Problem
Problem
Solving and
Algorithm
Design
Design
Page 4
Page Algorithm Alternatives
Algorithm
It’s possible to devise many algorithms to solve the same
It’s
problem.
problem.
Procedure Sat1231B
Set day_of_week to 12/31/2000
day_of_week
Alternative
Alternative
Set year to 2001
year
pseudocode to
While (year < 2021) Do
While
determine which
{
If year is a leap year
year
years in 20012020
Then increment day_of_week by 2 have New Year’s Eve
day_of_week
Else increment day_of_week by 1
day_of_week
on Saturday.
on
If day_of_week is Saturday
day_of_week
Then display year as having a Saturday New
year
Year’s Eve
Year’s
Increment year by 1
year
} Both algorithms work, but which is better?
better
Which is easier to code?
Which runs more efficiently? Chapter 6
Problem
Problem
Solving and
Algorithm
Design
Design
Page 5
Page Program Modularity
Program
The software development process
The
usually involves a team of developers, so
the software is often designed as a
hierarchical system of modules, which
can be viewed as easily modified
interdependent entities.
interdependent Chapter 6
Problem
Problem
Solving and
Algorithm
Design
Design
Page 6
Page Advantages of Modular
Programming
Programming Chapter 6
Problem
Problem
Solving and
Algorithm
Design
Design
Page 7
Page Computer Graphics Modularity
Example
Example
Initially, set the program up to
Initially,
calculate the scene with simple
lighting.
lighting.
main Replace the simple lighting with
Replace
more sophisticated reflective
surfaces.
surfaces.
main Add a module to smooth out the
Add
silhouettes of objects.
silhouettes
main generate
polygons draw
polygons generate
polygons draw
polygons generate
polygons generate
torus shade
polygon generate
torus blend
polygons generate
torus generate
cube
generate
teapot draw
pixels generate
cube
generate
teapot generate
sphere generate
sphere generate
cone generate
cone compute
reflect
draw
pixels generate
cube
generate
teapot
generate
sphere
generate
cone compute
curved
surfaces draw
polygons
blend
polygons
compute
reflect
draw
pixels
Chapter 6
Problem
Problem
Solving and
Algorithm
Design
Design
Page 8
Page TopDown Design
TopDown
One common approach for designing programs is the topOne
topdown methodology.
1. Design a highlevel solution to
level
the programming
problem.
problem. This approach
This
lends itself to
modularity.
modularity. 2. Consider each
Consider
complicated
subproblem as a
separate programming
problem.
problem. 3. Return to step
Return
#1 with each
subproblem.
subproblem. However, it may impose an artificial
However,
hierarchy on the tasks being
performed.
performed. Chapter 6
Problem
Problem
Solving and
Algorithm
Design
Design
Page 9
Page BottomUp Design
BottomUp
A newer approach for designing programs is the bottomup
newer
bottomup
methodology.
methodology.
1. Separate each major task
Separate
in the overall problem into
an individual programming
problem.
problem. 2. Develop cooperative
Develop
programming solutions
to the separate
problems.
problems. This approach
This
produces reusable However, those modules may prove
However,
modules.
modules.
difficult to cobble together to solve
bigger problems.
bigger Chapter 6
Problem
Problem
Solving and
Algorithm
Design
Design
Page 10
Page 10 ...
View
Full
Document
 Fall '08
 Klein,S
 Computer Science, Algorithms

Click to edit the document details