/****************************************** * * This program demonstrates the use of modulus and * division function, and the general programming style. * * Author: Xiannong Meng * Date: June 7, 2001 * ****************************************** */ #include using namespace std; const double SALES_TAX_RATE = 0.0875; // 8.75% sales tax int coinChange(int &, int); // function prototype void main(void) { int amountPaid, // amount paid to the seller itemCost, // how much it costs amount2Return, // 'change' to the customer quarters2Return, dimes2Return, // number of dimes to return nickles2Return, // number of nickles to return pennies2Return, // number of pennies to rerun totalCost; // final cost = item cost + tax cout << " Specify the cost (in cents e.g. 536 means 5 dollars 36 cents) : "; cin >> itemCost; cout << " Specify amount paid (in cents) : "; cin >> amountPaid; /* calculate the total cost in cents */ totalCost = int((itemCost * (1 + SALES_TAX_RATE)) + 0.5); /* calculate the change */ amount2Return = amountPaid - totalCost; /* print a message to the user */ cout << " Total cost (sales tax incl.) : " << totalCost << endl; cout << " Amount remitted : " << amountPaid << endl; cout << " Amount to return : " << amount2Return << endl; /* out of the amount to return, calculate how many quarters */ quarters2Return = coinChange(amount2Return, 25); /* how many dimes? */ dimes2Return = coinChange(amount2Return, 10); /* how many nickles? */ nickles2Return = coinChange(amount2Return, 5); /* finally how many pennies */ pennies2Return = coinChange(amount2Return, 1); /* print out the results */ cout << " Quarters returned : " << quarters2Return << endl; cout << " Dimes returned : " << dimes2Return << endl; cout << " Nickles returned : " << nickles2Return << endl; cout << " Pennies returned : " << pennies2Return << endl; } /* * function : int coinChange(int & change, int coinValue) * calculates the number of coins of a given face value * if the amount of change and the coin face value are given * input: 1) int change, amount of change * 2) int coinValue, face value of the coin * output:1) the number of coins to make up the change, returned * through function heading * 2) the change amount, through reference parameter */ int coinChange(int & change, int coinValue) { int numOfCoins; numOfCoins = change / coinValue; change = change % coinValue; return numOfCoins; }