Solution to Programming Assignment 2a
////
//
// This program makes change, given an amount of purchase and an amount
// tendered. The ouput is a total amount of change, followed by change in
// eight demoninations of money: twenties, tens, fives, dollars, quarters,
// dimes, nickels, and pennies. Both inputs are in dollars and decimal cents,
// e.g., 3.16 or .25. Output of the total change is in dollars and decimal
// cents, rounded to the nearest cent and printed always with exactly two
// places to the right of the decimal point, e.g., 1.84 or 0.10. Output of the
// change for each denomination is in whole numbers of that denomination.
//
// Author: Gene Fisher (gfisher@calpoly.edu)
// Created: 14apr99
// Modified: 14apr99
//
////
#include <iostream.h>
#include <iomanip.h>
int main() {
//
// Declare program variables to hold the purchase amount, amount tendered,
// total change amount, and the amounts of each denomination of change,
// from dollars to pennies.
//
float purchase; // Amount of purchase
float tendered; // Amount tendered
float change; // Total change due
int change_in_cents; // Total change due, in whole pennies
int twenties; // Number of twenties in change
int tens; // " " tens " "
int fives; // " " fives " "
int dollars; // " " dollars " "
int quarters; // " " quarters " "
int dimes; // " " dimes " "
int nickels; // " " nickels " "
int pennies; // " " pennnies " "
//
// Set up floating point output format always to print decimal point and
// not to use scientific notation.
//
cout.setf(ios::fixed, ios::floatfield);
cout.setf(ios::showpoint);
//
// Prompt the user for the amount of purchase.
//
cout << "Input the amount of the purchase, in dollars and cents: ";
//
// Input the amount of purchase.
//
cin >> purchase;
//
// Prompt for the amount tendered.
//
cout << "Input the amount tendered, in dollars and cents: ";
//
// Input the amount tendered.
//
cin >> tendered;
//
// Output a blank line, for nice formatting.
//
cout << endl;
//
// Compute the total amount of change due, in dollars and cents. Assume
// that this value is non-negative.
//
change = tendered - purchase;
//
// Output the total change amount, followed by a blank line. Note the use
// of setprecision to print exactly two places after the decimal point.
//
cout << "Total change due = " << setprecision(2) << change << endl
<< endl;
//
// Convert the total change in dollars and cents into whole number of
// cents. Note the rounding, which avoids round-off error that might
// result in floating point value. E.g., 3.14 in change might actually be
// represented as 3.1399999 in the floating point value of the change
// variable in computer memory.
//
change_in_cents = int (100 * (change + 0.005));
//
// Compute the number of twenties in change by dividing the total change by
// 2000.
//
twenties = change_in_cents / 2000;
//
// Compute the remaining amount of change by subtracting the amount of
// change in twenties from the total change.
//
change_in_cents = change_in_cents - (twenties * 2000);
//
// Proceed in the same way as for twenties with the remaining
// denominations.
//
tens = change_in_cents / 1000;
change_in_cents = change_in_cents - (tens * 1000);
fives = change_in_cents / 500;
change_in_cents = change_in_cents - (fives * 500);
dollars = change_in_cents / 100;
change_in_cents = change_in_cents - (dollars * 100);
quarters = change_in_cents / 25;
change_in_cents = change_in_cents - (quarters * 25);
dimes = change_in_cents / 10;
change_in_cents = change_in_cents - (dimes * 10);
nickels = change_in_cents / 5;
pennies = change_in_cents - (nickels * 5);
//
// Output the results of the pieces of change computations.
//
cout << "Change in twenties through pennies is:" << endl
<< " " << twenties << " twenties" << endl
<< " " << tens << " tens" << endl
<< " " << fives << " fives" << endl
<< " " << dollars << " dollars" << endl
<< " " << quarters << " quarters" << endl
<< " " << dimes << " dimes" << endl
<< " " << nickels << " nickels" << endl
<< " " << pennies << " pennies" << endl << endl;
return 0;
}