Respuesta :
Answer:
Below is the desired C++ program for the problem. Do feel free to edit it according to your preference
Explanation:
#include <iostream>
#include <vector>
using namespace std;
void ExactChange(int userTotal, vector<int> &coinVals) {
coinVals.reserve(5);
coinVals[0] = userTotal / 100;
userTotal %= 100;
coinVals[1] = userTotal / 25;
userTotal %= 25;
coinVals[2] = userTotal / 10;
userTotal %= 10;
coinVals[3] = userTotal / 5;
userTotal %= 5;
coinVals[4] = userTotal;
}
int main() {
vector<int> coins;
int value;
cin >> value;
if (value <= 0) {
cout << "no change" << endl;
} else {
ExactChange(value, coins);
if (coins[0] != 0) cout << coins[0] << " " << (coins[0] == 1 ? "dollar" : "dollars") << endl;
if (coins[1] != 0) cout << coins[1] << " " << (coins[1] == 1 ? "quarter" : "quarters") << endl;
if (coins[2] != 0) cout << coins[2] << " " << (coins[2] == 1 ? "dime" : "dimes") << endl;
if (coins[3] != 0) cout << coins[3] << " " << (coins[3] == 1 ? "nickel" : "nickels") << endl;
if (coins[4] != 0) cout << coins[4] << " " << (coins[4] == 1 ? "penny" : "pennies") << endl;
}
return 0;
}
There are several errors in the source program you attached; some of them include, invalid syntax for vector declaration and improper way of importing headers.
The fix to your program in C++ where comments are used as explanation is as follows:
The italicized lines are comments
#include<iostream>
#include<vector>
using namespace std;
//This defines the ExactChange as a void function
void ExactChange(int userTotal, vector<int> &coinVals) {
//This initializes coinNameSingular for the singular coin names
vector<string>coinNameSingular = {"dollar", "quarter", "dime", "nickel", "penny"};
//This initializes coinNamePlural for the plural coin names
vector<string>coinNamePlural = {"dollars", "quarters", "dimes", "nickels", "pennies"};
//This initializes the coin values as 0
vector<int>numCoins = {0,0,0,0,0};
//If the initial value of the total coin is 0 or less, this prints no change
if(userTotal <=0 ){ cout<<"No Change"; }
//If otherwise
else{
//This iterates through each coin
for(int i = 0;i<5;i++){
//This gets the value of the coin
numCoins[i] = userTotal/coinVals[i];
//This calculates the remaining value of userTotal
userTotal%=coinVals[i];
}
//This iterates through each coin again
for(int i = 0;i<5;i++){
/*The following if statement checks, if the value of coin is not 0
If yes the value of the coin is printed; it then checks if the value of the coin is 1. If it is 1, the singular coin is printed, else the plural coin is printed
If however, the value of the coin is 0, the statement does nothing*/
if (numCoins[i] != 0) cout << numCoins[i] << " " << (numCoins[i] == 1 ? coinNameSingular[i] : coinNamePlural[i]) << endl;
}
}
} // The ExactChange ends here
//The main function begins here
int main() {
//This initializes the value of each coin
vector<int>coinVals = {100, 25, 10, 2, 1};
//This declares the userTotal as integer
int userTotal;
//This iteration is repeated as long as the input for userTotal is valid
while(cin >> userTotal){
//This calls the ExactChange function for each valid value of userTotal
ExactChange(userTotal, coinVals);
}
return 0;
} // The program ends here
At the end of each valid input value, the program prints the corresponding values using the fewest number of coins.
See attachment for sample run
Read more about the fewest number of coin programs at:
https://brainly.com/question/16839801
![Ver imagen MrRoyal](https://us-static.z-dn.net/files/db2/254b453a2f5072def0162a9f511d4fc5.png)