Solution to Programming Assignment 3

Solution to Programming Assignment 3


////
//
// This program performs computations for a sales purchase.  The program starts
// with the input of a real number in dollars and cents for the amount of the
// purchase.  The program then inputs eight integers, for the number of each
// denomination of money available in the customer's wallet.  The
// denominations, in order of input, are: twenties, tens, fives, dollars,
// quarters, dimes, nickels, and pennies.
//
// The program computes an amount tendered that is greater than or equal to the
// amount of purchase, based on the money available in the wallet.  The amount
// tendered is output in the form of a list that indicates how many of each
// denomination of money from the wallet are to be paid out.
//
// The amount tendered output satisfies the following constraints:
//
//     a. There is no extra of any denomination in the amount tendered.  That
//        is, for any denomination for which one or more of that denomination
//        is tendered, there is no change back in that denomination.
//
//     b. The amount tendered output lists only those denominations that are
//        non-zero.
//
// If the wallet contains insufficient funds to cover the amount of purchase,
// then the program outputs how far short of the purchase amount the available
// funds are; this output is a real number of dollars and cents, in two decimal
// places.  If there are sufficient funds, then following the amount tendered
// output, the program outputs the total amount of change in dollars and
// fractional cents, to two decimal points of accuracy.
//
// Author: Gene Fisher (gfisher@calpoly.edu)
// Created: 14apr99
// Modified: 14apr99
//
////

#include <iostream.h>
#include <iomanip.h>
#include "debugging.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
    int purchase_in_cents;              // Purchase amount, in whole pennies
    int twenties;                       // Number of twenties  in wallet
    int tens;                           //   "    "  tens      "    "
    int fives;                          //   "    "  fives     "    "
    int ones;                           //   "    "  ones      "    "
    int quarters;                       //   "    "  quarters  "    "
    int dimes;                          //   "    "  dimes     "    "
    int nickels;                        //   "    "  nickels   "    "
    int pennies;                        //   "    "  pennies   "    "
    int twenties_tendered = 0;          // Number of twenties  tendered
    int tens_tendered = 0;              //   "    "  tens          "
    int fives_tendered = 0;             //   "    "  fives         "
    int ones_tendered = 0;              //   "    "  ones          "
    int quarters_tendered = 0;          //   "    "  quarters      "
    int dimes_tendered = 0;             //   "    "  dimes         "
    int nickels_tendered = 0;           //   "    "  nickels       "
    int pennies_tendered = 0;           //   "    "  pennies       "

    //
    // 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 for and input the amount of purchase.
    //
    cout << "Input the amount of the purchase, in dollars and cents: ";
    cin >> purchase;

    //
    // Prompt for and input the denominations in the wallet.
    //
    cout << "Input eight integers for the numbers of twenties through pennies in the wallet:"
         << endl << "    ";
    cin >> twenties >> tens >> fives >> ones
        >> quarters >> dimes >> nickels >> pennies;
    cout << endl;

    //
    // Compute the purchase in whole cents, rounding to the nearest cent.
    //
    purchase_in_cents = int(100 * (purchase + 0.005));

    //
    // If there are enough twenties to cover the purchase, take as many as
    // needed and we're done.  Otherwise, take as many as there are and press
    // on.
    //
    if (purchase_in_cents <= twenties * 2000) {
        twenties_tendered = purchase_in_cents / 2000;
        if (purchase_in_cents > twenties_tendered * 2000) {
            twenties_tendered = twenties_tendered + 1;
        }
    }
    else {
        twenties_tendered = twenties;
    }
    purchase_in_cents = purchase_in_cents - twenties_tendered * 2000;

    //
    // Ditto for tens, if there's still more to do.
    //
    if (purchase_in_cents > 0) {
        if (purchase_in_cents <= tens * 1000) {
            tens_tendered = purchase_in_cents / 1000;
            if (purchase_in_cents > tens_tendered * 1000) {
                tens_tendered = tens_tendered + 1;
            }
        }
        else {
            tens_tendered = tens;
        }
        purchase_in_cents = purchase_in_cents - tens_tendered * 1000;
    }

    //
    // Ditto for fives, if there's still more to do.
    //
    if (purchase_in_cents > 0) {
        if (purchase_in_cents <= fives * 500) {
            fives_tendered = purchase_in_cents / 500;
            if (purchase_in_cents > fives_tendered * 500) {
                fives_tendered = fives_tendered + 1;
            }
        }
        else {
            fives_tendered = fives;
        }
        purchase_in_cents = purchase_in_cents - fives_tendered * 500;
    }

    //
    // Ditto for ones, if there's still more to do.
    //
    if (purchase_in_cents > 0) {
        if (purchase_in_cents <= ones * 100) {
            ones_tendered = purchase_in_cents / 100;
            if (purchase_in_cents > ones_tendered * 100) {
                ones_tendered = ones_tendered + 1;
            }
        }
        else {
            ones_tendered = ones;
        }
        purchase_in_cents = purchase_in_cents - ones_tendered * 100;
    }

    //
    // Ditto for quarters, if there's still more to do.
    //
    if (purchase_in_cents > 0) {
        if (purchase_in_cents <= quarters * 25) {
            quarters_tendered = purchase_in_cents / 25;
            if (purchase_in_cents > quarters_tendered * 25) {
                quarters_tendered = quarters_tendered + 1;
            }
        }
        else {
            quarters_tendered = quarters;
        }
        purchase_in_cents = purchase_in_cents - quarters_tendered * 25;
    }

    //
    // Ditto for dimes, if there's still more to do.
    //
    if (purchase_in_cents > 0) {
        if (purchase_in_cents <= dimes * 10) {
            dimes_tendered = purchase_in_cents / 10;
            if (purchase_in_cents > dimes_tendered * 10) {
                dimes_tendered = dimes_tendered + 1;
            }
        }
        else {
            dimes_tendered = dimes;
        }
        purchase_in_cents = purchase_in_cents - dimes_tendered * 10;
    }

    //
    // Ditto for nickels, if there's still more to do.
    //
    if (purchase_in_cents > 0) {
        if (purchase_in_cents <= nickels * 5) {
            nickels_tendered = purchase_in_cents / 5;
            if (purchase_in_cents > nickels_tendered * 5) {
                nickels_tendered = nickels_tendered + 1;
            }
        }
        else {
            nickels_tendered = nickels;
        }
        purchase_in_cents = purchase_in_cents - nickels_tendered * 5;
    }

    //
    // Ditto for pennies, if there's still more to do.
    //
    if (purchase_in_cents > 0) {
        if (purchase_in_cents <= pennies * 1) {
            pennies_tendered = purchase_in_cents / 1;
            if (purchase_in_cents > pennies_tendered * 1) {
                pennies_tendered = pennies_tendered + 1;
            }
        }
        else {
            pennies_tendered = pennies;
        }
        purchase_in_cents = purchase_in_cents - pennies_tendered * 1;
    }


    //
    // If available funds are insufficient to cover the purchase, output an
    // error message and the amount of funds short.
    //
    if (purchase_in_cents > 0) {
        cout << "The funds are insufficient to cover the purchase, by "
             << setprecision(2) << purchase_in_cents / 100.0
             << endl << endl;
    }

    //
    // If there are sufficient funds, output the amount tendered in each
    // denomination, if the amount is non-zero.
    //
    else {
        cout << "Amount tendered:" << endl;
        if (twenties_tendered > 0) {
            cout << "    " << twenties_tendered << " twenties"
                 << endl;
        }
        if (tens_tendered > 0) {
            cout << "    " << tens_tendered << " tens"
                 << endl;
        }
        if (fives_tendered > 0) {
            cout << "    " << fives_tendered << " fives"
                 << endl;
        }
        if (ones_tendered > 0) {
            cout << "    " << ones_tendered << " ones"
                 << endl;
        }
        if (quarters_tendered > 0) {
            cout << "    " << quarters_tendered << " quarters"
                 << endl;
        }
        if (dimes_tendered > 0) {
            cout << "    " << dimes_tendered << " dimes"
                 << endl;
        }
        if (nickels_tendered > 0) {
            cout << "    " << nickels_tendered << " nickels"
                 << endl;
        }
        if (pennies_tendered > 0) {
            cout << "    " << pennies_tendered << " pennies"
                 << endl;
        }
        cout << endl;

        //
        // Output the total change due.
        //
        cout << "Total change due: "
             << setprecision(2)
             << -purchase_in_cents / 100.0
             << endl << endl;
    }

    return 0;
}


index | lectures | labs | handouts | assignments | solutions | grades | help