CS 1320 (Principles of Algorithm Design I):
Homework #3
- Assigned:
- February 4, 2000.
- Due:
- February 11, 2000, at start of class.
- Credit:
- 20 points (10 points per problem).
Instructions
Write a C++ program to solve each of the problems below.
Your code should begin with comments that describe
what the program is supposed to do, in terms of its inputs
and outputs.
See the sample programs
for examples.
Problems
Temperature conversion
Write a program that defines and tests a function to
convert Fahrenheit temperatures to Celsius
temperatures. Your program should include:
- A function that takes one input parameter, an
integer representing a Fahrenheit temperature,
and returns an integer representing
the equivalent Celsius temperature.
Recall from Homework #1 that the rule
for converting Fahrenheit
temperature F to Celsius temperature C
is: C = (5/9)(F - 32) .
- A main program -- similar to the one in example program
volume.cc --
that first applies the conversion function to a few
example inputs and prints the result, and then prompts
the user for one more input and prints the results of
converting that user-supplied input.
The program should print its results in a readable format.
You may want to define additional functions as well.
For each function you write, be sure to include comments
describing its precondition(s) and postcondition(s).
To help make the results of this conversion more accurate,
I have written a function divide_and_round that
divides one integer by another and returns a rounded result
(rather than the truncated result returned by the integer
division operator "/"). To use this function, you will need to:
To see how to use the function, read its comments in
divide.h.
Income tax estimate
U.S. income tax is a function of taxable income, calculated using
a formula such as the following
(adapted from the 1999 tax rate schedules for single taxpayers):
- If taxable income is not more than $25,750,
tax is 15% of taxable income.
- If taxable income is more than $25,750 but not more than
$62,450,
tax is $3,863 plus 28% of the amount over $25,750.
- If taxable income is more than $62,450 but not more than
$130,250,
tax is $14,139 plus 31% of the amount over $62,450.
- If taxable income is more than $130,250 but not more than
$283,150,
tax is $35,157 plus 36% of the amount over $130,250.
- If taxable income is more than $283,150,
tax is $90,201 plus 40% of the amount over $283,150.
Write a program that prompts the user for a taxable income
and computes and prints the tax due on that amount, based on
the above formula.
To do the computation, the program should use a function
that takes taxable income (in dollars, i.e.,
an integer) as a parameter and returns the tax on that amount,
also in whole dollars. Round to the nearest dollar.
You can do the whole calculation using only integers;
you may find the divide_and_round function,
discussed in the previous problem, helpful.
What to turn in
Submit your source code as described in the
guidelines for programming assignments.
For this assignment:
- Use a subject header of "cs1320 homework 3".
- Submit two source files, one for each problem.
Please choose names for the two files that indicate
which program solves which problem.
(For example,
you could use temperature.cc and tax.cc.)
You do not need to submit a copy of divide.h if
you use the divide_and_round function;
I will provide a copy of this file if it is needed to compile
and test your programs.