***need \"compute coin\" to compute the quarters, dimes, nickles,and pennies wit
ID: 3621548 • Letter: #
Question
***need "compute coin" to compute the quarters, dimes, nickles,and pennies with a call by reference, can someone modify?
#include
using namespace std;
void computeCoin(int, int&, int&);
void display(int cents, int q, int d, int n, int p);
void computeCoin(int denomination, int& n, int& leftover)
{
n = leftover / denomination;
leftover %= denomination;
}
void display(int c, int q, int d, int n, int p)
{
cout<<" ";
cout << c << " cents can given be given as" << endl
<< q << " quarter(s) " << d << " dime(s) " << n << " nickel(s) "
<< p << " penny(pennies)";
}
bool Continue()
{
char decision;
cout<<" Would like to try again with a different amount (Y/N)?: ";
cin>>decision;
cout<<" ";
return (decision=='y')||(decision=='Y');
}
int main()
{
int cents, coin_value, number=0, amount_left;
const int QUARTERS=25, DIMES=10, NICKELS=5, PENNIES=1;
int numQuarters=0, numDimes=0, numNickels=0, numPennies=0;
do
{
cout << "Please enter the coin value (ranging from 1 to 99 cents): ";
cin >> cents;
while (cents < 1 || cents > 99)
{
cout << " Not a valid amount. Please enter a valid value.";
cout << " Please enter value of cents one more time (from 1 to 99 cents): ";
cin >> cents;
}
amount_left = cents;
numQuarters = numDimes = numNickels = numPennies = 0;
while(amount_left > 0)
{
if (amount_left / QUARTERS != 0)
coin_value = QUARTERS;
else if (amount_left / DIMES != 0)
coin_value = DIMES;
else if (amount_left / NICKELS != 0)
coin_value = NICKELS;
else
coin_value = PENNIES;
computeCoin(coin_value, number, amount_left);
if (coin_value == QUARTERS)
numQuarters = number;
else
if (coin_value == DIMES)
numDimes = number;
else if (coin_value == NICKELS)
numNickels = number;
else
numPennies = number;
}
display(cents, numQuarters, numDimes, numNickels, numPennies);
} while (Continue());
cout<<"Thank You.";
return 0;
}
Explanation / Answer
#include
using namespace std;
void compute_coin(int coin_value, int &number, int ¢s);
void input (int ¢s);
int main()
{
int cents;
int number;
char choice;
do
{
input (cents);
cout <<< " cents can given be given as: " << endl;
compute_coin (25, number, cents);
cout<<<" Quarter(s)"<
compute_coin (10, number, cents);
cout<<<" Dime(s)"<
compute_coin (5, number, cents);
cout<<<" Nickel(s)"<
compute_coin (1, number, cents);
cout<<<" Penny(Pennies) "<
cout<<"Would you like to try again with a different amount (Y/N)? ";
cin>>choice;
}while(choice=='Y'||choice=='y');
cout<<" Thank You.";
return 0;
}
void compute_coin(int coin_value, int &number, int ¢s)
{
number=cents/coin_value;
cents=cents%coin_value;
}
void input (int ¢s)
{
cout<<"Please enter the coing value (ranging from 1 to 99 cents): ";
cin>>cents;
cout<
while (cents < 1 || cents > 99)
{
cout << " Not a valid amount. Please enter a valid value.";
cout << " Please enter value of cents one more time (from 1 to 99 cents): ";
cin >> cents;
cout<
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.