0

Below is my code which is supposed to print the number of coins needed as per greedy algorithm.

Here when I enter, let us say 5.5, it outputs 22 which is fine. But when I enter 5.6 or 10.6, the program kind of freezes. It does not even print "here" which is below do while loop in the main function.

But when I comment calculateCoins(change), it do prints "here" in the printf statement in the main loop. What I might be missing which may be causing this behaviour. Moreover, when I upload the code and check the same using check 50, I get the output as:

enter image description here

So it means that it is not the issue related to the system but the issue is with the code.

Comments are openly invited :)

void calculateCoins(float money);

int main ()
{
    float change;

    do
    {
        printf("\nO hai! How much change is owed?\n");
        change=GetFloat();
        //scanf("%f",&change);
    } while (change <= 0);

    printf("here");
    calculateCoins(change);
}

void calculateCoins(float money)
{
    int coins=0;
    float i=0;
    if(money>=0.25)
    {
        i=fmod(money , 0.25);
        coins+=(int)(money/0.25);
    }
    else if (money>=.10)
    {
        i=fmod(money , 0.1);
        coins+=(int)(money/0.1);
    }
    else if (money>=.05)
    {
        i=fmod(money , 0.05);
        coins+=(int)(money/0.05);
    }
    else if (money>=.01)
    {
        i=fmod(money , 0.01);
        coins+=(int)(money/0.01);
    }
    if(i!=0)
    {
        do
        {
            if(i>=0.25)
            {
                i=fmod(i , 0.25);
                coins+=(int)(i/0.25);
            }
            else if (money>=.10)
            {
                i=fmod(i , 0.1);
                coins+=(int)(i/0.1);
            }
            else if (money>=.05)
            {
                i=fmod(i , 0.05);
                coins+=(int)(i/0.05);
            }
            else if (money>=.01)
            {
                i=fmod(i , 0.01);
                coins+=(int)(i/0.01);
            }
        } while (i > 0);
    }

    printf("%d\n",coins);
}
0

First of all, your statement that here is not printed when 5.6 or 10.6 are given as inputs is wrong. It does print here. The reason your program freezes, is because it goes in an infinite loop. Let's see how your program will work if 5.6 is given as input.

  1. It prints O hai! How much change is owed?
  2. It reads 5.6 in change.
  3. It prints here.
  4. It calls calculateCoins().
  5. coins gets initialized to 0.
  6. i gets initialized to 0.
  7. money is 5.6 so is greater that 0.25 and the if statement is true.
  8. i takes the value fmod(5.6, 0.25) which is 0.1.
  9. i is not 0 so the if statement is true and we go into the do-while loop.
  10. i is not greater or equal to 0.25 so the if statement is false.
  11. i is greater that or equal to 0.1 so the first if else statement is true.
  12. Now i gets the value fmod(0.1, 0.1) which should be 0. Right? Right!?

Well no.

Let's see what really happens at this last step. I'm gonna insert

printf("i = %.50f\n", i);

after the i=fmod(i , 0.1); line inside your do-while loop.

And here is what gets printed:

i = 0.09999990463256835937500000000000000000000000000000

So it turns out it's not exactly 0.1 heh?

And so fmod(0.09999990463256835937500000000000000000000000000000, 0.1) is still 0.09999990463256835937500000000000000000000000000000 (since it's smaller than 0.1) and so, i doesn't change value, resulting in the infinite loop.

All that could have been avoided if you had firstly watched the lectures and followed the instructions before trying without thinking first. Follow the aforementioned instructions (i.e. watch the lectures and read the specification) and you'll learn about the float imprecision in machine memory and that you should have used the round() function to convert the change in cents, which should be an int (which unlike float is precise). After that if you have any more questions, we'll be here to help again.


If this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the check-mark. And don't forget to keep coding!

3
  • 1
    I am giving this reply really late as I saw notification today. The explanation was really a detailed one and is appreciable. But you really need to understand one thing, while coding we tend to make mistakes and sometimes could not see some basic faults. Yes I watched the lectures and at that time was very well aware of floating point imperfections, but somehow overlooked that. You could have either simply responded to the query or may have ignored it, but getting rude and angry was a strange move. Why answer to a query if it makes you angry. Anyway, thank you for the response ( a late one) May 5 '19 at 20:00
  • Seeing my answer after more than 3 years, I'll have to agree with you. Sorry for my tone, it was uncalled for. I don't know what was going through my mind at that point. I hope you have succeeded and you are doing something you like right now. :) Happy coding!
    – ChrisG
    May 6 '19 at 9:09
  • 1
    Amazingly yes. Hope you too are pursuing what you always desired. Happy coding :). And ya, really no harsh feelings or whatsoever.. good that you responded back. Take care May 6 '19 at 13:49

You must log in to answer this question.

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