Word Problems

Programmers are rarely handed mathematical expressions to turn into programs. Instead they typically receive informal problem descriptions that often contain irrelevant and sometimes ambiguous information. The programmers' first task is to extract the relevant information and then to formulate appropriate expressions. Here is a typical example:
Company XYZ & Co.\ pays all its employees $12 per hour. A typical employee works between 20 and 65 hours per week. Develop a program that determines the wage of an employee from the number of hours of work.
The last sentence is the first to mention the actual task: to write a program that determines one quantity based on some other quantity. More specifically, the program consumes one quantity, the number of hours of work, and produces another one, the wage in dollars. The first sentence implies how to compute the result, but doesn't state it explicitly. In this particular example, though, this poses no problem. If an employee works h hours, the wage is

#displaymath72464#

Now that we have a rule, we can formulate a Scheme program:

<#744#>(d<#744#><#745#>efine<#745#> <#746#>(wage<#746#> <#747#>h)<#747#>
  <#748#>(*<#748#> <#749#>12<#749#> <#750#>h))<#750#> 
The program is called <#60392#><#754#>wage<#754#><#60392#>; its parameter <#60393#><#755#>h<#755#><#60393#> stands for the hours an employee works; and its result is <#60394#><#756#>(*<#756#>\ <#757#>12<#757#>\ <#758#>h)<#758#><#60394#>, the corresponding wage. external ~<#760#>Discuss with your students how they teased out what they had to do and what information was superfluous. It is impossible to give guidelines on that, but practice and discussing the idea repeatedly helps a lot. The following exercises prepare students thematically and practically for the section on ``a program = many function definitions''. Use the solutions as motivations for the section on comopsing functions.<#760#>
<#763#>Exercise 2.3.1<#763#> Utopia's tax accountants always use programs that compute income taxes even though the tax rate is a solid, never-changing 15. Define the program <#60395#><#766#>tax<#766#><#60395#>, which determines the tax on the grosspay. Also define <#60396#><#767#>netpay<#767#><#60396#>. The program determines the netpay of an employee from the number of hours worked. Assume an hourly rate of $12.~ external Solution<#60397#><#60397#> <#773#>Exercise 2.3.2<#773#> The local supermarket needs a program that can compute the value of a bag of coins. Define the program <#60398#><#775#>sum-coins<#775#><#60398#>. It consumes four numbers: the number of pennies, nickels, dimes, and quarters in the bag; it produces the amount of money in the bag.~ external Solution<#60399#><#60399#> <#781#>Exercise 2.3.3<#781#> An old-style movie theater has a simple profit function. Each customer pays $5 per ticket. Every performance costs the theater $20, plus $.50 per attendee. Develop the function <#60400#><#783#>total-profit<#783#><#60400#>. It consumes the number of attendees (of a show) and produces how much income the attendees produce.~ external Solution<#60401#><#60401#>