I have a program where a user inputs an amount due, and an amount paid. A function makeChange(double amount) is passed the difference (paid - due) and returns a double array with the change due broken down by denomination.
double* makeChange(double amount) {
double* change = new double[10];
double values[10] = { 100, 50, 20, 10, 5, 1, .25, .10, .05, .01 };
double remaining = amount;
for (int i = 0; i < 10; i++) {
change[i] = floor(remaining / values[i]);
remaining -= (change[i] * values[i]);
}
return change;
}
I do not always get the output expected. For example, when I pass (0.05 - 0.00) as the amount, it returns saying I need 1 nickel. But if I pass (4.05 - 4.00) through as the amount, it returns saying I need 4 pennies.
What in the code is causing this to happen? Thanks!