Write a program that tells what coins to give out for any amount of change from
ID: 3621154 • Letter: W
Question
Write a program that tells what coins to give out for any amount of change from 1 cent to 99 cents. You should use one function called get_input, to get the input from the user and another function called make_change that uses a call-by-reference mechanism to “pass back” how many quarters, dimes, nickels and pennies are needed.You should write a third function called display_results to actually do the printing to the screen. You may write other functions as well, if you like.
For example if the amount is 86 cents, then the input would be 86 and the output would be something like the following:
86 cents can be given as
3 quarters, 1 dime, 0 nickels and 1 penny
Include a loop that lets the user repeat this computation for new input values until the user says he wants to quit. DO NOT USE GLOBAL VARIABLES.
Explanation / Answer
/**********************************************************
Program Name:-Coin_program.cpp
Author:
Class:
Course:
Date:
**********************************************************/
#include <iostream>//Declaration of header files
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>
using namespace std;
void subject(); //display the main aim of the program
void computeCoin(int, int&, int&); //function to compute coins
void display(int cents, int q, int d, int p); // display coins to be given
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
{
subject();//call to functin sunbject()
cout << "Please Enter the coin_value (ranging from 1 to 99 cents): ";
cin >> cents;
while (cents < 1 || cents > 99)
{
cout << " Invalid Entry.Please enter a valid entry...";
cout << " Enter cents (from 1 to 99 cents): ";//Enter a valid values of cents from user
cin >> cents;
}
amount_left = cents; // cents will be used later
numQuarters = numDimes = numPennies = 0; // reset all to zero
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;
computeCoin(coin_value, number, amount_left);//call to function computeCoin
if (coin_value == QUARTERS)
numQuarters = number;
else
if (coin_value == DIMES)
numDimes = number;
else
numPennies = number;
}
display(cents, numQuarters, numDimes, numPennies);//call to function display
cout << " Do you want to continue? ";
cout <<" Press y to continue or n to stop! ";
cin >> more;
} while (tolower(more) == 'y');//Continue if the user presses y or Y
system("PAUSE");
return 1;
} // end main()
///////////////////////////////////////////////////////////
///////////////////////////
// display the main aim of the program //
///////////////////////////
void subject()
{
cout << "This program tells what coins to give out for any "
<< "amount of change from 1 cent to 99 cents. ";
} // end subject()
///////////////////////////////////////////////////////////
/////////////////////////////////////////
// compute # of coins per denomination //
/////////////////////////////////////////
void computeCoin(int denomination, int& n, int& leftover)
{
n = leftover / denomination;
leftover %= denomination;
} // end computeCoin()
///////////////////////////////////////////////////////////
//////////////////////////////////////////
// display what coins and # to be given //
//////////////////////////////////////////
void display(int c, int q, int d, int p)
{
cout.put(' ');
cout << c << " cents can given be given as" << endl
<< q << " quarter(s) " << d << " dime(s) and "
<< p << " penny(pennies)";
} // end output()
///////////////////////////////////////////////////////////
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.