Skip to main content
Lesson 4 - Simple I/O
Lesson MenuPreviousNext
  
L.A.4.1 - Change page 9 of 10

Background:

Some cash register systems use change machines that automatically dispense coins. 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                  Example Values:

temp = cashPaid - purchaseAmount;     8.06 = 30.00 - 21.94 
temp = temp - (int)temp;              0.06 = 8.06 - 8
change = (int)(temp * 100);           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:

double purchaseAmount, cashPaid, temp;
int change;

...	data input stuff                  Example Values:

temp = cashPaid - purchaseAmount;     8.06 = 30.00 - 21.94 
temp = temp - (int)temp + 0.00001;    0.06000998 = 8.05999998 - 8 + 0.00001
change = (int)(temp * 100);           6 = (int)(0.06000998 * 100)

Assignment:

  1. Write a program that does the following:

    1. Prompts the user for the following information.
      Amount of purchase
      Amount of cash tendered
    2. 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 apcslib.

  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 save the run output, cut and paste the run output to your source code, but do not include user prompts. Only copy the relevant answer section of the run output window.


Lesson MenuPreviousNext
Contact
 ©ICT 2003, All Rights Reserved.