LAB EXERCISE
CHANGE
Background:
Some cash register systems use automatic change machines where the coinage is automatically dispensed. This lab will investigate the problem solving and programming behind such machinery. You should use integer mathematics to solve this problem.
You will need to extract the amount of cents from dollar amounts expressed in real numbers. This will require using the type cast operator and dealing with the approximate nature of real number storage. Here is an important example:
double purchaseAmount, cashPaid, temp;
int change;
... data input stuff
temp = cashPaid - purchaseAmount; temp = temp - int (temp); change = int (temp * 100);
|
Example Values:
8.06 = 30.00 - 21.94 0.06 = 8.06 - 8 6 = int (0.06 * 100) |
However, when the above example was run on a computer, the answer of 5 was given. Because real numbers are stored as approximations, the value of 0.06 was actually something like 0.05999998. Because the type conversion of int (0.05999998 * 100) will truncate the fractional part, the result is erroneously 5. We need to make a minor adjustment to the second line:
... data input stuff
temp = cashPaid - purchaseAmount; temp = temp - int (temp) + 0.00001; change = int (temp * 100);
|
Example Values:
8.06 = 30.00 - 21.94 0.06000998 = 8.05999998 - 8 + 0.00001 6 = int (0.06000998 * 100) |
Assignment:
1. Write a program that does the following:
a. Prompts the user for the following information.
Amount of purchase
Amount of cash tendered
b. Calculates and prints out the coinage necessary to make correct change. Do not solve the amount of bills required in the change amount.
2. Sample run output:
Amount of purchase = 23.06
Cash tendered = 30.00
Amount of coins needed:
94 cents =
3 quarters
1 dime
1 nickel
4 pennies
3. Include appropriate documentation in your program.
4. You are encouraged, but not required, to use the formatting tools in <iomanip.h>.
5. Do not worry about singular versus plural endings, i.e. quarter/quarters.
Instructions:
1. Complete and run the program and verify the calculations. Use the values given on page one.
2. When it comes time to send the run output to disk, cut and paste the run output to your source code, do not include user prompts. Only copy the relevant answer section of the run output window.