Write a function called calculateChange with total change amount as an integer input and returns the change using the fewest coins. The coin types are dollars, quarters, dimes, nickels, and pennies.
Write a function called calculateChange that takes six parameters all of type integer: change dollars as pass by reference, quarters as pass by reference, dimes as pass by reference, nickels as pass by reference, pennies as pass by reference,
Ensure the arguments are in that order. The function calculates the correct number of each coin type using the fewest coins and returns these via the pass by reference parameters.
The main function of the program inputs the change amount, calls the function and then displays the number for each coin with one coin type per line.
Example sessions:
2933
Dollars: 29
Quarters: 1
Dimes: 0
Nickels: 1
Pennies: 3
Follow the specification carefully. As stated above, make sure to use "integer" as the data type for the variables.
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
/*
1) Write a program with total change amount as an integer input that outputs the change using the fewest coins, one coin type per line.
2) The coin types are dollars, quarters, dimes, nickels, and pennies.
3) Use singular and plural coin names as appropriate, like 1 penny vs. 2 pennies.
4) Your program must define and call the following function.
void ExactChange(int userTotal, vector<int>& coinVals)
5) Positions 0-4 of coinVals should contain the number of dollars, quarters, dimes, nickels, and pennies, respectively.
*/
void ExactChange(int userTotal, vector<int>& coinVals);
const int PENNIES_IN_DOLLAR = 100, PENNIES_IN_QUARTER = 25, PENNIES_IN_DIME = 10, PENNIES_IN_NICKEL = 5;
int main() {
int userTotal;
cin >> userTotal;
if (userTotal == 0) {
cout << "no change" << endl;
}
else {
vector<int> coinVals;
ExactChange(userTotal, coinVals);
if (coinVals[0] > 0) {
cout << coinVals[0];
if (coinVals[0] > 1) {
cout << " dollars" << endl;
} else {
cout << " dollar" << endl;
}
}
if (coinVals[1] > 0) {
cout << coinVals[1];
if (coinVals[1] > 1) {
cout << " quarters" << endl;
} else {
cout << " quarter" << endl;
}
}
if (coinVals.at(2) > 0) {
cout << coinVals[2];
if (coinVals[2] > 1) {
cout << " dimes" << endl;
}else {
cout << " dime" << endl;
}
}
if (coinVals[3] > 0) {
cout << coinVals[3];
if (coinVals[3] > 1) {
cout << " nickels" << endl;
}else {
cout << " nickel" << endl;
}
}
if (coinVals[4] > 0) {
cout << coinVals[4];
if (coinVals[4] > 1) {
cout << " pennies" << endl;
}else {
cout << " penny" << endl;
}
}
}
return 0;
}
void ExactChange(int userTotal, vector<int>& coinVals) {
int dollars = userTotal / PENNIES_IN_DOLLAR;
userTotal %= PENNIES_IN_DOLLAR;
int quarters = userTotal / PENNIES_IN_QUARTER;
userTotal %= PENNIES_IN_QUARTER;
int dimes = userTotal / PENNIES_IN_DIME;
userTotal %= PENNIES_IN_DIME;
int nickels = userTotal / PENNIES_IN_NICKEL;
userTotal %= PENNIES_IN_NICKEL;
int pennies = userTotal;
coinVals.resize(5);
coinVals[0] = dollars;
coinVals[1] = quarters;
coinVals[2] = dimes;
coinVals[3] = nickels;
coinVals[4] = pennies;
}
Comments
Does not work!!! Does not work!!!
Leave a comment