In: Computer Science
6.32 LAB: Exact change - functions Write a program with total change amount as an integer input that outputs the change using the fewest coins, one coin type per line. The coin types are dollars, quarters, dimes, nickels, and pennies. Use singular and plural coin names as appropriate, like 1 penny vs. 2 pennies. Ex: If the input is: 0 or less, the output is: no change Ex: If the input is: 45 the output is: 1 quarter 2 dimes Your program must define and call the following function. Positions 0-4 of coinVals should contain the number of dollars, quarters, dimes, nickels, and pennies, respectively. void ExactChange(int userTotal, vector& coinVals) In C++ please
Since there are no constraints given, it is assumed that there are infinite number of all coins available.
C++ code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void ExactChange(int userTotal, vector<int>&
coinVals){
vector<string> coinsName(4, "");
coinsName[0] = "Quaters"; coinsName[1] =
"Dimes"; coinsName[2] = "Nickels"; coinsName[3] = "Pennies";
for(size_t i = 0; i < coinVals.size();
++i)
{
int numberOfCoins =
userTotal / coinVals[i]; // calculate the number of quarters
userTotal = userTotal %
coinVals[i]; // calculate remaining change needed
cout <<
coinsName[i] << ": " << numberOfCoins << endl; //
display # of quarters
}
}
int main ( )
{
float userNUmber;
int change, quarters, dimes, nickels, pennies;
// declare variables
cout <<"Enter the amount of money:
";
cin >> userNUmber; // input the amount of
change
change = userNUmber;
vector<int> coins(4, 0);
coins[0] = 25; coins[1] = 10; coins[2] = 5;
coins[3] = 1;
ExactChange(change, coins);
return (0);
}
Please refer to the screenshot of the code to understand the indentation of the code
Output:
Hope this helped. Please do upvote and if there are any queries please ask in comments section.