Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms IV Top-Down Design.

Similar presentations


Presentation on theme: "Algorithms IV Top-Down Design."— Presentation transcript:

1 Algorithms IV Top-Down Design

2 Top-Down Design Also known as Top-Down Stepwise Refinement.
Big problem becomes a set of smaller problems Smaller problems become even smaller problems Finally a pile of simple problems, each of which is easy to solve.

3 Top-Down Design, Idealized Divide and Conquer
Problem P Subproblem P1 Subproblem P2 P22 P11 P12 P21 P111 P112 P121 P122 P211 P212 P221 P222 P1111 P1112 P1121 P1122 P1211 P1212 P1221 P1222 P2111 P2112 P2121 P2122 P2211 P2212 P2221 P2222

4 Example Design Compute Income Tax
Goal: compute income tax Steps Read and verify income. Read and verify filing status Compute tax Print income, filing status, and tax

5 Example Design Compute Income Tax
First high-level pseudocode read and verify income. read and verify filing status compute tax print income, filing status, and tax

6 Characteristics of a Good Decomposition
“Clean” --- Tasks don’t overlap. All tax computations are in the “Compute Tax” section. “Not all modules are equal” Modules are divided according to function, not size.

7 Writing Pseudocode: Read and verify income
while entered income is incorrect ask the user to enter a correct income value end_while

8 Read and verify income set INCORRECT to -1 /* income must be non-negative */ set MININCOME to /* minimum income for this program */ set income to INCORRECT while INCOME is INCORRECT ask the user to enter a correct income value set INCOME to the value entered ask the user to verify INCOME if the user does not verify INCOME then set INCOME to INCORRECT else if INCOME is less than MININCOME then end_if end_while

9 Read and verify filing status
print the filing status menu while entered filingStatus is incorrect ask the user to enter a correct filing status end_while

10 Read and verify filing status (II)
set correctStatusInput to ‘n’ /* this is an indicator variable */ print filing status menu /* “n” means “no” or “bad” */ while correctStatusInput is equal to ‘n’ print “Enter filing status: should be between 1 and 4: “ read status if status is greater than 0 if status is less than 5 set correctStatusInput to ‘y’ end_if end_if end_while

11 Print filing status menu
print “Single ” print “Married filing jointly ” print “Married filing separately ” print “Head of household ”

12 Compute tax (A) if status is 1
compute tax = x (income ) end_if if status is 2 if income is less than or equal to 82,150 compute tax = x (income ) else compute tax = x (income )

13 Compute tax (B) if status is 3
compute tax = x (income ) end_if if status is 4 if income is less then or equal to compute tax = x (income ) else compute tax = x (income )

14 Print income, filing status, and tax
print verbal description of filing status print tax

15 Print income, filing status, and tax (II)
if status is equal to 1 print “Single” end_if if status is equal to 2 print “Married filing jointly”

16 Print income, filing status, and tax (IIA)
if status is equal to 3 print “Married filing separately” end_if if status is equal to 4 print “Head of household” print tax

17 Summary Use the divide-and conquer strategy to split the original problem into a series of sub-problems. Consider each sub-problem separately and split them further into smaller sub-problems, progressively refining the previous versions in a top-to-bottom manner, until no further refinement is possible. Stop the process when you have the pseudocode that can be directly translated in a C program.

18 Summary (II) Combine all final refinements of subprograms according to the logic of the original program.


Download ppt "Algorithms IV Top-Down Design."

Similar presentations


Ads by Google