VIEWS: 0 PAGES: 5 POSTED ON: 1/13/2012
TA C252 :Computer Programming II Top-down Design • Design Approach Lec-2 • Principle – Divide and Conquer Today’s Agenda • Steps • Top-Down Design – Divide the problem into sub-problems • Top-Down Design Examples – Solve the sub-problems • The Function Concept – Combine the (sub-)solutions 12-Aug-11 1 12-Aug-11 2 Top-down Design Task • How do you solve the sub-problem? • Divide-and-conquer again! • Steps Subtask1 Subtask2 Subtask3 1. Divide the problem into sub-problems 2. Repeat step 1 for each sub-problem until the problem is small enough to have an atomic solution. Subtask1.1 Subtask1.2 3. Combine the solutions. 12-Aug-11 4 12-Aug-11 3 Top-Down Design Advantages of Top-Down Design • If we look at a problem as a whole, it may seem • Breaking the problem into parts helps us to clarify impossible to solve because it is so complex. what needs to be done. Examples: • At each step of refinement, the new parts become – writing a tax computation program less complicated and, therefore, easier to figure – writing a word processor out. • Complex problems can be solved using top-down • Parts of the solution may turn out to be reusable. design, also known as stepwise refinement, where • Breaking the problem into parts allows more than – We break the problem into parts one person to work on the solution. – Then break the parts into parts – Soon, each of the parts will be easy to do 1 An Example of Top-Down Design The Top Level • Get the customer list from a file. • Problem: • Sort the list according to zip code. – We own a home improvement company. • Make a new file of only the customers with the zip – We do painting, roofing, and basement code 21222 from the sorted customer list. waterproofing. • Print an envelope for each of these customers. – A section of town has recently flooded (zip code 21222). – We want to send out pamphlets to our Main customers in that area. Read Sort Select Print Another Level? Structured Programs • Should any of these steps be broken down • We will use top-down design for all remaining further? Possibly. programming problems. • How do I know? Ask yourself whether or not • This is the standard way of writing programs. you could easily write the algorithm for the • Programs produced using this method and using step. If not, break it down again. only the three kinds of control structures, sequential, selection and repetition, are called • When you are comfortable with the structured programs. breakdown, write the pseudocode for each of • Structured programs are easier to test, modify, the steps (modules) in the hierarchy. and are also easier for other programmers to • Typically, each module will be coded as a understand. separate function. Another Example The Top Level • Problem: Write a program that draws this • Draw the outline of the house picture of a house. • Draw the chimney • Draw the door • Draw the windows Main Draw Draw Draw Draw Outline Chimney Door Windows 2 Pseudocode for Main Observation • The door has both a frame and knob. We could Call Draw Outline break this into two steps. Call Draw Chimney Main Call Draw Door Call Draw Windows Draw Draw Draw Draw Outline Chimney Door Windows Draw Draw Door Frame Knob Pseudocode for Draw Door Another Observation Call Draw Door Frame • There are three windows to be drawn. Call Draw Knob Main Draw Draw Outline . . . Windows Draw Draw Draw Window 1 Window 2 Window 3 One Last Observation Reusing the Window Code • But don’t the windows look the same? They just have different locations. • So, we can reuse the code that draws a window. Main – Simply copy the code three times and edit it to place the window in the correct location, or Draw Draw – Use the code three times, “sending it” the correct Outline . . . Windows location each time (we will see how to do this later). • This is an example of code reuse. Draw a Window 3 Pseudocode for Draw Windows Top-Down Design – Example 1 Call Draw a Window, sending in Location 1 • Problem: Compute a solution to a quadratic Call Draw a Window, sending in Location 2 equation. Call Draw a Window, sending in Location 3 – Input: Real Numbers a,b,c – Output: x such that ax^2 + bx + c = 0 • Fact: – x = (-b + sqrt(b^2 – 4ac)) / (2a) is a solution. 12-Aug-11 20 Top-Down Design – Example 1 Top-Down Design – Example 2 Compute x s.t. ax^2+bx+c=0 • Problem: Compute the sum of n natural numbers Compute (-b + sqrt(b^2 – 4ac)) / (2a) – Input: n – Output: sum(n) = 1 + 2 + ……+n-1 + n Compute (-b + d) / (2a) Compute d = sqrt(b^2 – 4ac) 12-Aug-11 21 12-Aug-11 22 Example2: Sum(n) =1+2+ ….(n-1)+n Top-Down Design – Example 3 • Compute xy where y is a power of 2. Sum(n-1) + n • Input: x and y • Output: xy + Sum(n-2) n-1 ………………. 12-Aug-11 23 12-Aug-11 24 4 Example 3 – xy • Consider xy, where y=2k xy xy/2 * xy/2 * * xy/4 … xy/4 … 12-Aug-11 25 5