# Pset1- GREEDY PROBLEM

``````while((a!=0)&&(a>0))
{
printf("LOOP ENTERED\n");
if(a>=0.25)
{
a=a-0.25;
i++;
}

else if((a<0.25)&&(a>=0.10))
{
a=a-0.10;
i++;
}
else if((a<0.10)&&(a>=0.05))
{
a=a-0.05;
i++;
}
else if((a<0.05)&&(a>=0.01))
{
a=a-0.01;
i++;
}
}
``````

why is this an infinite loop? IS this approach wrong. I have initialized i=0 at start. And a is the amount in dollars.

• Additional to @Cliff B's answer let me give you some advice about your code. Firstly in your while loop, since you check for `a > 0` you don't have to check for `a != 0` too, as it's included in the `>` operator. Secondly in your consecutive if statements, if `a` is bigger or equal to 0.25 it will enter the first if and it WILL NOT check the other ifs. Moreover if `a` is less than 0.25 it will check the second if, so you don't have to check again if `a` is less that 0.25. What I'm trying to say is you could write i like that: `if (a >= 0.25)` and then `else if (a >= 0.10)` and so on. Jul 10 '15 at 8:09
• Yeah. That makes sense. Thank you :) (y) Jul 10 '15 at 17:41
• It's nothing. You can also mark @Cliff B's answer as correct (by clicking the gray check icon next to the answer) if it helped you solve your problem. Jul 10 '15 at 17:42

The purpose of this exercise is to understand the issues with inexact storage of floats. You've run into exactly this problem. In your code, a will never get all the way to zero. To see what's happening, insert the following line in your while loop:

`````` printf("a=%.20f.   ",a);
``````

For a more detailed explanation, see this answer:

pset1 Greedy can't compute pennies

So, that should get you going. If you're happy with this, please mark your question as answered. Let's keep up on forum housekeeping. ;-)

• Ohk cool! Thank you. I had one more doubt.. How do you check the output for foo or any string? Jul 10 '15 at 17:39
• Since this question doesn't call for string output, not sure I follow. Sounds like a new question. IF so, feel free to post a new one. Jul 10 '15 at 19:01