3. Algorithm
Flowcharts have applications in
many areas other than
computer solutions
For example, in how-to-build-it
instructions and recipes
An algorithm is a step-by-
step procedure for solving a
problem
4. Algorithm (cont’d)
An algorithm for baking a chocolate
cake is the chocolate cake recipe
An algorithm for repairing your
Mustang carburetor is the instructions
in the shop manual
A flowchart is a “picture” of the
algorithm
If it is done well, it is like a road map,
and the path to the solution will be
smooth
5. Algorithm (cont’d)
A computer program is a set of
instructions that tells a computer to
solve a problem in a step-by-step
manner
The program is written from a logic
representation
The model we employ when using the
computer to find a solution is first the
algorithm and then the “picture”
A flowchart
6. Algorithm (cont’d)
From the flowchart we form a
computer program, which we
use to arrive at the solution
We can check the solution either
by hand or by running the
program
Here are two analogous stages to
problem solving (with the
computer and without)
7. Algorithm (cont’d)
Without Computer With Computer
1. What is the problem? Same
2. Make a model of the problem Develop an algorithm
3. Analyze the model Use a flowchart?
4. Find the solution Write a computer program
5. Check the solution Run the program
8. Problem Solving…
Continuing with our analysis of
higher-level problem solving
techniques, today we will examine
the concept of Top Down Design
Problem solving often involves
breaking a problem down into
smaller more manageable parts
Then deal with each part
individually
9. Top Down Design
This method is well suited to
problem solving
It allows us to start with general
steps and elaborate on them
until the problem is solved
The process of making the steps
more and more specific in top
down design is called stepwise
refinement
10. Let’s Try it…
(1.) The problem:
Building a barn
This is a big
project, so we
break the project
down into smaller
steps
11. Top Down Design (cont’d)
Let’s Build
a Barn
Build
Foundation
Build
Walls
Build
Roof
Now continue with the step wise
refinement, breaking down each
section again
12. Top Down Design (cont’d)
Build
a Barn
Build
Foundation
Build
Walls
Build
Roof
step wise refinement could continue
even more, but you see the pattern
Dig Concrete Framing Paneling Rafters Shingles
13. Top Down Design (cont’d)
Hierarchy charts do not indicate
the flow of data
But instead illustrate the tasks,
or modules, for each part of the
solution
The only flow of data important
is what should go into and come
out of each module
14. Another Example…
An ATM machine…
ATM Machine
Get Inputs Give Outputs
Perform all
Calculations
First level hierarchy chart
15. The ATM example (cont’d)
Second level hierarchy chart for input
Get Inputs
Get Password
Get Transaction
Amount
Get Transaction
Type
16. The ATM example (cont’d)
Second level hierarchy chart for output
Get Outputs
Print Error
Message
Dispense Cash
Print Balance
17. The ATM example (cont’d)
Second and third level hierarchy chart for calculations
Perform
Calculations
Handle
Deposits
Handle
Withdrawals
Add amount
to balance
Check
balance
Subtract amount
from balance
Give an error
message
18. The ATM example (cont’d)
The complete
hierarchy
chart for the
ATM
Perform all
calculations
Handle
Deposits
Handle
Withdrawals
Add amount
to balance
Check
balance
Subtract amount
from balance
Give an error
message
Give
outputs
Print error
message
Print
balance
Dispense
cash
Give
Inputs
Get
password
Get
transaction
Get
amount
ATM
19. Top Down Design (cont’d)
The use of top down
design, or hierarchy
charts is truly a divide
and conquer method
of problem solving
20. Logical Representations
A logical representation is some sort
of model that represents processes in
the real world
In some sense, mathematics is a model
that represents the logic of natural
systems
We have already used top down design
diagrams and other logic representations
Now we’ll examine another method
commonly used in computer problem
solving, flowcharts
22. Flowchart Symbols
The common flowchart symbols
Start/Stop – necessary for
every flowchart
Operation – do something
Arrow – shows direction of
flow
23. Flowchart Symbols (cont’d)
Decision – answer yes
or no
Information in – “read
in”
Information out –
“output box”
Connector – used for
larger flowcharts
25. Solution…
First, list all necessary steps (algorithm)
Do not take anything for granted
Second, put the steps in the correct
order
1. Walk to the room
2. Stop at the door
3. Take keys from pocket
4. Choose correct key
5. Put key in lock
28. Another one…
The problem…
You are in a house with a
telephone, a doorbell, and a
alarm clock
You hear one of
them ringing,
what is the
solution
29. Solution…
START
Listen to the
bell ringing
Is it the
telephone?
Answer the
phone
Is it the
doorbell?
See who is at
the door
Turn off the
alarm
STOP
N Y
Y N
30. “Decision Symbol”
With this symbol, there is always
one arrow coming in, but two
arrows (the decision) coming out
?
No
Yes
33. Last one…
The problem…
Leonardo Fibonacci of Pisa wrote in
1202 about the number of rabbits that
would be found at the end of a year if
we started with a single pair of rabbits
The pair produced one pair of offspring
after the second month
Then they stopped breeding
Each new pair also produced
two more pairs in the same
way then stopped breeding
34. Problem (cont’d)
The answer to the first year is as follows
Number of
additional pairs
1 1 2 3 5 8 13 21 34 55 89 144
Month 1 2 3 4 5 6 7 8 9 10 11 12
The first 2 months are both 1 (since there
is only one additional pair)
Each month after, is the sum of the 2
months before it
This is know as the Fibonacci sequence
35. Problem (cont’d)
Looking at it mathematically…
F1 = 1 This translates as “first month (F1) equals one pair”
F2 = 1 This translates as “second month (F2) equals one
additional pair”
F3 = F2 + F1
= 1 + 1
This translates as “third month (F3) equals second month
plus first month”
F4 = F3 + F2
= 2 + 1
= 3
Etc.
There is a definite pattern, and thus we
have found an algorithm to find all the
terms
36. Start
Let the old
Term equal 0
Let the newest
Term equal 1
Find sum of
Old and new term
Is sum
> 100?
Let old term equal
The newest term
Let the newest
Term equal the sum
Write out
The sum
Stop
Y
N
Solution…
For numbers up to
100