/* * File: makechange.cpp * Author: Robert I. Pitts * Last Modified: September 17, 2000 * Topic: Functions * ---------------------------------------------------------------- * * This program determines the number of dollars, quarters, dimes, * nickels, and pennies to give as change. The program expects the * amount of change to make (in cents) as input. * */ #include /************************ Function Prototypes ************************/ // Function: makeChange // Usage: makeChange(cents, dollars, quarters, dimes, nickels, pennies) // In: cents // Out: dollars, quarters, dimes, nickels, pennies // // Determines the amount of each unit to give as change. Assumes // you want to give as many of the larger coins/bills as possible // before giving smaller ones. void makeChange(int cents, int &dollars, int &quarters, int &dimes, int &nickels, int &pennies); /*************************** Main Program **************************/ int main() { int cents, dollars, quarters, dimes, nickels, pennies; cout << "How much change to make (in cents): "; cin >> cents; makeChange(cents, dollars, quarters, dimes, nickels, pennies); cout << "\nYour change is:" << endl << dollars << " dollars" << endl << quarters << " quarters" << endl << dimes << " dimes" << endl << nickels << " nickels" << endl << pennies << " pennies" << endl; } /*********************** Function Definitions **********************/ // Function: makeChange // // Uses integer division (quotient) and modulus (remainder) to determine // the number of each unit to give as change. void makeChange(int cents, int &dollars, int &quarters, int &dimes, int &nickels, int &pennies) { dollars = cents / 100; // How many units of 100 cents (quotient). cents = cents % 100; // Cents left over (remainder). quarters = cents / 25; cents = cents % 25; dimes = cents / 10; cents = cents % 10; nickels = cents / 5; cents = cents % 5; pennies = cents; }