I'm writing what should be a simple programme for an assignment that finds the ideal change to dispense, measured in the number of coins. My solution is to use a a while loop that keeps checking through coins and subtracting them from the customer change until change equals 0. However, I am getting stuck in an infinite loop and am completely stumped as to why.
Here is my code:
while (change > 0.0) {
for (int i = 0; i < numberOfCoins; i++) {
if ((change - coins[i]) >= 0.0) {
change = change - coins[i];
coinsTally[i] = coinsTally[i] + 1;
break;
}
}
}
change
is any value from 0.0
to 0.99
, numberOfCoins = 6
,
and coins[numberOfCoins] = { 0.5, 0.2, 0.1, 0.05, 0.02, 0.01}