Algorithms IV: Top-Down Design - PowerPoint PPT Presentation

About This Presentation
Title:

Algorithms IV: Top-Down Design

Description:

from GBurt's text ... Topics more pseudocode problem solving strategies Home-Exercise: The Box Home-Exercise: Drawing a Rectangle – PowerPoint PPT presentation

Number of Views:157
Avg rating:3.0/5.0
Slides: 21
Provided by: csUmbcEd
Category:
Tags: algorithms | design | down | top

less

Transcript and Presenter's Notes

Title: Algorithms IV: Top-Down Design


1
Algorithms IV Top-Down Design
  • Topics
  • more pseudocode
  • problem solving strategies
  • Home-Exercise The Box
  • Home-Exercise Drawing a Rectangle
  • Reading
  • None

2
Pseudocode and Then Some
  • We can write an algorithm for finding the average
    of two integers
  • 1. Get an integer, ltnum1gt, from the user
  • 2. Get an integer, ltnum2gt, from the user
  • 3. ltSumgt ltnum1gt ltnum2gt
  • 4. ltaveragegt ltsumgt / 2
  • 5. Display The average of ltnum1gt and
    ltnum2gt is ltaveragegt
  • Steps 1, 2 5 will make use of code found in the
    run-time library.

3
Investigating Steps 1 5
  • Get an integer, ltnum1gt, from the user
  • Display The average ltaveragegt.
  • Are these really single steps?
  • Each of them is really a sequence of steps, a
    subprogram. Two kinds of subprograms
  • Functions - an answer is returned to the main
    program.
  • Procedures - no answer returned to the main
    program

4
Functions and Procedures
  • In our example, the function that gets a value
    from the user and the procedure that prints out
    the answer were already written.
  • Many times it is necessary for us to write our
    own procedures or functions.

5
Problem Solving Strategies
  • If we look at the problem as a whole it may seem
    impossible, because it is so complex.
  • Complex problems can be solved using a Top-Down
    approach aka Stepwise refinement
  • Break the problem into parts
  • Then break the parts into parts
  • Soon each of the parts will be easy to do

6
Advantages of Top-down Design
  • Breaking the problem down into parts helps us to
    clarify what needs to be done.
  • At each step of refinement, the new parts become
    less complicated and therefore easier to figure
    out.
  • What if youve broken a problem down as much as
    you can and you still dont know?
  • Missing information
  • More detail is needed

7
A Practical Example
  • Scenario
  • We own a home improvement company
  • We do painting, roofing and basement
    waterproofing
  • A section of town has recently had flooding
  • We want to send out waterproofing pamphlets to
    our customers in that area

8
Our Algorithm
  • Get the customer list from a file
  • Sort the list according to zipcode
  • Make a new file of only the customers with the
    zipcode 21222 from the sorted customer list
  • Print an envelope for each customer in the new
    file

9
Single steps ?
  • None of our steps are single steps
  • Employ Top-Down Design
  • Start with the large idea and break into large
    parts
  • Each part will end up being a function or a
    procedure

10
Pseudocode for our Main Program
  • Call ltGet Customer Listgt
  • Call ltSort on customer listgt by zipcode
  • Call ltMake Target Listgt from customer list using
    only zipcode 21222
  • Call ltPrint Envelopesgt using target list

11
The Next Step
  • Write algorithms for each of the functions or
    procedures.
  • These may include steps that are really several
    steps, too.
  • Give names to these new functions and procedures.
  • Write algorithms for them.
  • Continue this process until done.

12
Structured Programs
  • Top-down design will be used for all of our
    programming projects
  • This is the standard way of writing programs.
  • Programs produced using this method and using
    only the 3 kinds of structures, sequential,
    selection and repetition are called structured
    programs.
  • Structured programs are easier to modify later
    and also easier for other programmers to
    understand.

13
Another Example
  • Problem Write a program that draws this picture
    of a house.

14
The Algorithm
  • Draw the outline of the house
  • Draw the chimney
  • Draw the door and doorknob
  • Draw the windows

15
Similarities
  • All three windows are identical.
  • They are simply in different positions.
  • We can write a function called ltDrawWindowgt that
    will draw one window at a position we specify and
    call it 3 times with the three different
    positions.
  • Another benefit of Top-down design is
    reuseability. We are reusing the code for
    ltDrawWindowgt.

16
The Pseudocode
  • Call ltDrawHouseOutlinegt giving a position
  • Call ltDrawChimneygt giving position
  • Call ltDrawDoorgt giving position
  • Call ltDrawWindowgt giving position 1
  • Call ltDrawWindowgt giving position 2
  • Call ltDrawWindowgt giving position 3

17
Problem Solving Techniques
  • We have investigated three problem solving
    techniques
  • Multiple attacks
  • Working backwards
  • Top-down design

18
Writing Algorithms from Scratch
  • Do the following 2 projects as exercises at home
  • Given a problem statement, write the
    corresponding generic algorithm for the solution.
  • Use the following procedure
  • Determine the algorithm inputs and outputs
  • Pseudocode a rough algorithm
  • Complete the pseuodocode

19
The Box
  • Problem Write an interactive program to compute
    and display the volume and surface area of a box.
    The program must also display the box
    dimensions. Error checking should be done to be
    sure that all box dimensions are greater than
    zero.

20
Drawing a Rectangle
  • Problem Write an interactive program that will
    draw a solid rectangle of asterisks (). The
    program must also display the dimensions of the
    rectangle. Error checking must be done to be
    sure that the dimensions are greater than zero.
Write a Comment
User Comments (0)
About PowerShow.com