11
More Algorithm Design CSIS 1595: Fundamentals of Programming and Problem Solving 1

More Algorithm Design CSIS 1595: Fundamentals of Programming and Problem Solving 1

Embed Size (px)

Citation preview

Page 1: More Algorithm Design CSIS 1595: Fundamentals of Programming and Problem Solving 1

More Algorithm Design

CSIS 1595: Fundamentals of Programming and Problem Solving 1

Page 2: More Algorithm Design CSIS 1595: Fundamentals of Programming and Problem Solving 1

Example

• Requirements:Write program to compute income taxes from gross income and dependents.– Tax is a tax rate (currently 20%) of deductible

income.– Deductible income is gross income minus

deductions.– Deductions include a standard deduction

(currently $10,000) and a $3000 dependent deduction rate for each dependent.

Page 3: More Algorithm Design CSIS 1595: Fundamentals of Programming and Problem Solving 1

Algorithm Design in Terms of Data

• What information do you need to keep track of?– Look for nouns in requirements– These will often become your variables

• Where will that information come from?– Input by user?– Specified in advance?– Computed from something else?• If so, how?

Page 4: More Algorithm Design CSIS 1595: Fundamentals of Programming and Problem Solving 1

Variables in Example

tax deductible income × tax ratetax_rate Set at 20%deductible_income gross income minus deductionsgross_income Input by userdeductions standard deduction + dependents × dependent deduction ratestandard_deduction Set at 10000dependents Input by userdependent_deduction Set at 3000

Page 5: More Algorithm Design CSIS 1595: Fundamentals of Programming and Problem Solving 1

Algorithm Design in Terms of Steps

• Order of steps often determined by data dependency• Cannot compute variable until values it depends on is

known– Assign variables with set values– Prompt user for variables they must provide– Compute remaining variables in order of

dependencies

Page 6: More Algorithm Design CSIS 1595: Fundamentals of Programming and Problem Solving 1

Dependencies in Example

• Set tax_rate, standard_deduction, and dependent_deduction_rate values

• Prompt user for gross_income and dependents values

• Compute deductions from standard_deduction, dependents, and dependent_deduction

• Compute taxable_income from gross_income and deductions

• Compute tax from taxable_income and tax_rate

Page 7: More Algorithm Design CSIS 1595: Fundamentals of Programming and Problem Solving 1

Example Pseudocode

Page 8: More Algorithm Design CSIS 1595: Fundamentals of Programming and Problem Solving 1

Constants

• Many values considered “named constants”– Do not change value during program– May change value in future versions of program

(so should be easy to change in future)

• Define named constant at beginning of code– Convention: Use all caps to distinguish from variablesTAX_RATE = 0.2

– Use named constant in program instead of valuetax = taxable_income * TAX_RATE

Page 9: More Algorithm Design CSIS 1595: Fundamentals of Programming and Problem Solving 1

Scaffold Testing

• Problem: Only output we have for testing is final tax value

• If not correct, error could come from any (or several) previous calculations– Computation of deductions– Computation of taxable income– Computation of tax

Page 10: More Algorithm Design CSIS 1595: Fundamentals of Programming and Problem Solving 1

Scaffold Testing

• Solution: Print all intermediate values as they are computed– deduction– taxable_income

• Make sure these computed in advance for test

• Can comment out when finished– Do not delete, since may need in future!

Page 11: More Algorithm Design CSIS 1595: Fundamentals of Programming and Problem Solving 1

Scaffold Testing