Making Change Download

Dave Wittry

classes, casting, strings Student approved


Introduction

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 example:

double purchaseAmount, cashPaid, temp;
int change;
... data input stuff
temp = cashPaid - purchaseAmount;
temp = temp – (int) (temp);
change = (int) (temp * 100);

// Example Values (as done in a math class):
// 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 change comes out incorrect. You must investigate why and solve the problem.

You should solve this part of the problem FIRST, before you go on and calculate the correct number of quarters, dimes, nickels, and pennies.


Task

  1. With a random number generator, generate the amount of purchase, including where the dollars part is a random integer from 1 to 10 and where the cents part is a random real number from 0.0 to 0.99.
  2. Prompts the user for the amount of cash tendered (read as a real number)
  3. Calculates and prints out the coinage necessary to make correct change. You should also echo the original data entered back to the user and report the number of whole dollars needed for change as well. You can assume that there will always be some coins returned
  4. You should format the output in some nice manner.
  5. You should correctly print singular versus plural endings (i.e., quarter/quarters). Use an “if” statement – this is the ONLY time you are allowed to use an if statement in this lab.
  6. You must print the $ before the dollar amount and the ¢ after the cents.


Starter code

class Change{
	
}

Expected output

INPUT:
Amount of purchase: 23.06
Cash tendered: 30.00

OUTPUT:
Amount of Purchase: 23.06
Cash Tendered: 30.00 
Dollars returned: $6
Coins returned: 94¢ = 3 quarters
1 dime
1 nickel
4 pennies

Useful tools to solve the problem