Algorithms IV TopDown Design CMSC 104 Peter Olsen

  • Slides: 18
Download presentation
Algorithms IV Top-Down Design CMSC 104: Peter Olsen, Fall 99 Lecture 10: 1

Algorithms IV Top-Down Design CMSC 104: Peter Olsen, Fall 99 Lecture 10: 1

Top-Down Design • Also known as Top-Down Stepwise Refinement. • Big problem becomes a

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. CMSC 104: Peter Olsen, Fall 99 Lecture 10: 2

Top-Down Design, Idealized Divide and Conquer Problem P Subproblem P 1 Subproblem P 2

Top-Down Design, Idealized Divide and Conquer Problem P Subproblem P 1 Subproblem P 2 P 1111 P 1121 P 1122 P 1211 CMSC 104: Peter Olsen, Fall 99 P 122 P 1212 P 1221 P 22 P 211 P 1222 P 2111 P 212 P 221 P 2112 P 2121 P 2122 P 2211 P 2212 P 2221 P 2222 Lecture 10: 3

Example Design Compute Income Tax • Goal: compute income tax • Steps • •

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 CMSC 104: Peter Olsen, Fall 99 Lecture 10: 4

Example Design Compute Income Tax • First high-level pseudocode read and verify income. read

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 CMSC 104: Peter Olsen, Fall 99 Lecture 10: 5

Characteristics of a Good Decomposition l “Clean” --- Tasks don’t overlap. o All tax

Characteristics of a Good Decomposition l “Clean” --- Tasks don’t overlap. o All tax computations are in the “Compute Tax” section. l “Not all modules are equal” o Modules are divided according to function, not size. CMSC 104: Peter Olsen, Fall 99 Lecture 10: 6

Writing Pseudocode: Read and verify income while entered income is incorrect ask the user

Writing Pseudocode: Read and verify income while entered income is incorrect ask the user to enter a correct income value end_while CMSC 104: Peter Olsen, Fall 99 Lecture 10: 7

Read and verify income set INCORRECT to -1 /* income must be non-negative */

Read and verify income set INCORRECT to -1 /* income must be non-negative */ set MININCOME to 50000 /* 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 set INCOME to INCORRECT end_if CMSC 104: Peter Olsen, Fall 99 Lecture 10: 8 end_while

Read and verify filing status print the filing status menu while entered filing. Status

Read and verify filing status print the filing status menu while entered filing. Status is incorrect ask the user to enter a correct filing status end_while CMSC 104: Peter Olsen, Fall 99 Lecture 10: 9

Read and verify filing status (II) set correct. Status. Input to ‘n’ /* this

Read and verify filing status (II) set correct. Status. Input to ‘n’ /* this is an indicator variable */ print filing status menu /* “n” means “no” or “bad” */ while correct. Status. Input 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 correct. Status. Input to ‘y’ end_if end_while CMSC 104: Peter Olsen, Fall 99 Lecture 10: 10

Print filing status menu print “FILING STATUS MENU: ” print “Single ---- 1” print

Print filing status menu print “FILING STATUS MENU: ” print “Single ---- 1” print “Married filing jointly ---- 2” print “Married filing separately ---- 3” print “Head of household ---- 4” CMSC 104: Peter Olsen, Fall 99 Lecture 10: 11

Compute tax (A) if status is 1 compute tax = 11158. 50 + 0.

Compute tax (A) if status is 1 compute tax = 11158. 50 + 0. 31 x (income - 49300. 00) end_if if status is 2 if income is less than or equal to 82, 150 compute tax = 5100. 00 + 0. 28 x (income - 34000. 00) else compute tax = 18582. 00 + 0. 31 x (income - 82150. 00) end_if CMSC 104: Peter Olsen, Fall 99 Lecture 10: 12

Compute tax (B) if status is 3 compute tax = 9291. 00 + 0.

Compute tax (B) if status is 3 compute tax = 9291. 00 + 0. 31 x (income - 41075. 00) end_if if status is 4 if income is less then or equal to 70450. 00 compute tax = 4095. 00 + 0. 28 x (income - 27300. 00) else compute tax = 16177. 00 + 0. 31 x (income - 70450. 00) end_if CMSC 104: Peter Olsen, Fall 99 Lecture 10: 13

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

Print income, filing status, and tax print income print verbal description of filing status print tax CMSC 104: Peter Olsen, Fall 99 Lecture 10: 14

Print income, filing status, and tax (II) Print income if status is equal to

Print income, filing status, and tax (II) Print income if status is equal to 1 print “Single” end_if if status is equal to 2 print “Married filing jointly” end_if CMSC 104: Peter Olsen, Fall 99 Lecture 10: 15

Print income, filing status, and tax (IIA) if status is equal to 3 print

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” end_if print tax CMSC 104: Peter Olsen, Fall 99 Lecture 10: 16

Summary • Use the divide-and conquer strategy to split the original problem into a

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. CMSC 104: Peter Olsen, Fall 99 Lecture 10: 17

Summary (II) • Combine all final refinements of subprograms according to the logic of

Summary (II) • Combine all final refinements of subprograms according to the logic of the original program. CMSC 104: Peter Olsen, Fall 99 Lecture 10: 18