In C++ Write the code for a function called computeChange that will determine ho
ID: 3904581 • Letter: I
Question
In C++
Write the code for a function called computeChange that will determine how many quarters, dimes, and nickels will be returned to the customer for the purchase of a Vendltem in Question 3. Assume the globally declared array changeCoins always hold the 3 values for the change coin categories. Position O will be the number of quarters in the change, position 1 will be the number of dimes in the change, and position 2 will be the number of nickels in the change. The function is passed the price of the item and the cash tendered for the item. (HINT An algorithm could be to subtract 25 cents from the change repeatedly until less than 25 cents remains, then subtract 1O cents repeatedly, then 5 cents, counting each successful subtraction in the appropriate position of the array.) const int COIN-TYPES = 3; int changecoins[COIN TYPES]://globally declared and accessible void computeChange(double price, double cash)Explanation / Answer
Code is given below for the question. Please do rate if helpful. Thank you
void computeChange(double price, double cash)
{
double change = cash - price; //calculate change to be returned
changeCoins[0] = 0; //initialize number of quarters to 0
while(change >= 0.25)
{
change = change - 0.25 ; //reduce 1 quarter
changeCoins[0]++;
}
changeCoins[1] = 0; //initialize number of dimes to 0
while(change >= 0.10)
{
change = change - 0.10 ; //reduce 1 dime
changeCoins[1]++;
}
changeCoins[2] = 0; //initialize number of nickels to 0
while(change >= 0.05)
{
change = change - 0.05 ; //reduce 1 nickel
changeCoins[2]++;
}
}