Write a C++ program tol calculate the minimum number of coins (quarters, dimes,
ID: 671139 • Letter: W
Question
Write a C++ program tol calculate the minimum number of coins (quarters, dimes, nickels, and pennies) required to make change. It will perform this calculation for three separate input values.
Write at least two functions. One function will read one value in cents, make change, and print the results. The other function is given an amount and a coin value and returns the number of coins used of that value and the remaining amount. You may write additional functions, but you must write at least these two functions.
Input Specification
There will be three positive integer values (in cents) that should be converted into coins. You should read each value separately, convert it to coins, and print the result before reading the next value. You should include a short prompt. You may assume that the values are non-negative.
Output Specification
The output will be three lines of the form:
54 cents can be made with 2 quarters, 0 dimes, 0 nickels, and 4 pennies
Your solution must contain a function that has a no parameters and a void return value.
This function reads one value, makes change for that value (not manually), and prints the number of each coin used. It does this by calling another function. To use the minimum number of coins, start with quarters and work your way down to pennies. You should call this function three times from main.
Your solution must also include a function that computes the number of a specific coin that can be taken from a given amount. This function has three parameters. It uses integer division and modulus division. The return type of this function should be void. This function MUST include two reference parameters and one value parameter.
The first parameter is the amount remaining to be changed into coin. It is a reference parameter because its value is passed both in and out. At the end of the function, it is the amount remaining after the coins current size has been removed.
The second parameter is the value of the current coin in cents. This value is passed into the function.
The third parameter is the number of coins of the current size that have been taken from the input amount to be changed. This has no value passed in but a value passed out.
Sample Execution
Enter a value to change into coin: 17
17 cents can be made with 0 quarters, 1 dimes, 1 nickels, and 2 pennies.
Enter a value to change into coin: 29
29 cents can be made with 1 quarters, 0 dimes, 0 nickels, and 4 pennies.
Enter a value to change into coin: 72
72 cents can be made with 2 quarters, 2 dimes, 0 nickels, and 2 pennies.
Explanation / Answer
#include <iostream>
using namespace std;
void changemaker(int *pennies, int coin, int *numCoins)
{
*numCoins = *pennies / coin;
*pennies = *pennies % coin;
}
int main()
{
int cents, quarters, dimes, nickels, pennies;
for(int i = 0; i < 3; i++)
{
cout<<"Enter a value to change into coin: ";
cin>>cents;
pennies = cents;
changemaker(&pennies, 25, &quarters);
changemaker(&pennies, 10, &dimes);
changemaker(&pennies, 5, &nickels);
cout<<cents<<" cents can be made with "<<quarters<<" quarters, "<<dimes<<" dimes, "<<nickels<<" nickels, and "<<pennies<<" pennies."<<endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.