0

plz help. When testing greedy, all numbers appeared to work except 2.2. I get 10 coins instead of 11.

int owed = cash * 100;

    for (i = owed; i > 24; i = i - quarter )
    {
    coins++;
    owed = owed - quarter;
    }

    for (i = owed; i > 9; i = i - dime )
    {
    coins++;
    owed = owed - dime;
    }

    for (i = owed; i > 4; i = i - nickel )
    {
    coins++;
    owed = owed - nickel;
    }

    for (i = owed; i > 0; i = i - penny )
    {
    coins++;
    owed = owed - penny;
    }

printf("Number of coins need:%i\n", coins);
1

Have you added a few debug outputs for showing the values of cash and owed?

I'm asking as I assume you might have a problem with floating point not being able to exactly represent the input number, and want to round the result of cash * 100 before assigning it to owed (otherwise the number is truncated, and 13.99999999 becomes 13).

2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .