Solution to Programming Assignment 1

Solution to Programming Assignment 1


////
//
// 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 five
// demoninations of money: dollars, quarters, dimes, nickels, and pennies.  All
// inputs and outputs are in integer cents.
//
// Author: Gene Fisher (gfisher@calpoly.edu)
// Created: 21mar99
// Modified: 2apr99
//
////

#include <iostream.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.
    //
    int purchase;                       // Amount of purchase
    int tendered;                       // Amount tendered
    int change;                         // Total change due
    int dollars;                        // Number of dollars  in change
    int quarters;                       //   "    "  quarters "    "
    int dimes;                          //   "    "  dimes    "    "
    int nickels;                        //   "    "  nickels  "    "
    int pennies;                        //   "    "  pennnies "    "

    //
    // Prompt the user for the amount of purchase.
    //
    cout << "Input the amount of the purchase, in cents: ";

    //
    // Input the amount of purchase.
    //
    cin >> purchase;

    //
    // Prompt for the amount tendered.
    //
    cout << "Input the amount tendered, in cents: ";

    //
    // Input the amount tendered.
    //
    cin >> tendered;

    //
    // Output a blank line, for nice formatting.
    //
    cout << endl;

    //
    // Compute the total amount of change due, in cents.  Assume that this
    // value is non-negative.
    //
    change = tendered - purchase;

    //
    // Output the total change amount, followed by a blank line.
    //
    cout << "Total change due = " << change << endl
         << endl;

    //
    // Compute the number of dollars in change by dividing the total change by
    // 100.
    //
    dollars = change / 100;

    //
    // Compute the remaining amount of change by subtracting the amount of
    // change in dollars from the total change.
    //
    change = change - (dollars * 100);

    //
    // Proceed in the same way as for dollars with quarters, dimes, nickels,
    // and pennies.
    //
    quarters = change / 25;
    change = change - (quarters * 25);
    dimes = change / 10;
    change = change - (dimes * 10);
    nickels = change / 5;
    pennies = change - (nickels * 5);

    //
    // Output the results of the pieces of change computations.
    //
    cout << "Change in dollars through pennies is:" << endl
         << "    " << dollars << " dollars" << endl
         << "    " << quarters << " quarters" << endl
         << "    " << dimes << " dimes" << endl
         << "    " << nickels << " nickels" << endl
         << "    " << pennies << " pennies" << endl << endl;

    return 0;
}


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