Anda di halaman 1dari 3

Write a program that tells what coins to give out for any amount of change f rom 1 cent to 99 cents.

For example, if the amount is 86 cents, the output would be something like the following: 86 cents can be given out as 3 quarter(s) 1 dime(s) and 1 penny(pennies) Use coin denomination of 25 cents (quarters), 10 cents (dimes), and 1 cent ( pennies). Do not use nickel and half-dollar coins. Your program will use the fol lowing functions (among others): void compute_coins(int coin_value, int& number, int& amount_left); // Precondition: 0 < coin_value < 100; 0 <= amount_left < 100. /* Postcondition: number has been set equal to the maximum number of coins o f denomination coin_value cents that can be obtained from amount_left cents. Amo unt_Left has been decreased by the value of the coins, i.e., decreased by number *coin_value. */ For example, suppose the value of the variable amount_left is 86. Then, afte r the following call, the value of number will be 3 and the value of amount_left will be 11 (because if you take 3 quarters from 86 cents, that leaves 11 cents) : compute_coins(25, number, amount_left); Include a loop that lets the user repeat this computation for new input valu es until the user says he or she wants to end the program. Hint: use integer div ision and % operator to implement this function.

#include #include #include #include

<iostream.h> <stdlib.h> <ctype.h> <conio.h> // print topic // compute coins // display coins to be given

void topic(); void compute_coin(int, int&, int&); void output(int cents, int q, int d, int p); int main() { int cents, coin_value, number=0, amount_left; const int QUARTERS=25, DIMES=10, PENNIES=1; int numQuarters=0, numDimes=0, numPennies=0; char more; do {

topic(); cout << "Enter coin_value (from 1 to 99 cents): "; cin >> cents; while (cents < 1 || cents > 99) { cout << "\nInvalid Entry..."; cout << "\nEnter cents (from 1 to 99 cents): "; cin >> cents; }

amount_left = cents; numQuarters = numDimes = numPennies = 0; while(amount_left > 0) { if (amount_left / QUARTERS != 0) coin_value = QUARTERS; else if (amount_left / DIMES != 0) coin_value = DIMES; else coin_value = PENNIES;

// cents will be used later // reset all to zero

compute_coin(coin_value, number, amount_left); if (coin_value == QUARTERS) numQuarters = number; else if (coin_value == DIMES) numDimes = number; else numPennies = number; } output(cents, numQuarters, numDimes, numPennies); cout << "\n\nContinue (y/n)? "; cin >> more; } while (tolower(more) == 'y'); return EXIT_SUCCESS; } // end main() /////////////////////////////////////////////////////////// /////////////////////////// // display program topic // /////////////////////////// void topic() { clrscr(); cout << "This program tells what coins to give out for any\n" << "amount of change from 1 cent to 99 cents.\n\n"; } // end topic() /////////////////////////////////////////////////////////// ///////////////////////////////////////// // compute # of coins per denomination // ///////////////////////////////////////// void compute_coin(int denomination, int& n, int& leftover) { n = leftover / denomination; leftover %= denomination; } // end compute() ///////////////////////////////////////////////////////////

////////////////////////////////////////// // display what coins and # to be given // ////////////////////////////////////////// void output(int c, int q, int d, int p) { cout.put('\n'); cout << c << " cents can given be given as" << endl << q << " quarter(s) " << d << " dime(s) and " << p << " penny(pennies)"; } // end output() ///////////////////////////////////////////////////////////

Anda mungkin juga menyukai