0

So I have been working on greedy, but it seems to not work properly. When I run this:

{ 
int x = 0;

float f;
do{
printf("How much?");
f= GetFloat();
float roundf(float f); 
}
while(f<0);

    for(float i_1= f;1<=i_1; i_1--)
    {
        x++;
        f--;
    }
    for(float i_2 = f;0.50<=i_2; i_2--)
    {
        x++;
        f=f-0.5;
    }
    for(float i_3 = f;0.25<=i_3; i_3--) 
    {
        x++;
        f=f-0.25;
    }      
    for(float i_4 = f;0.10<=i_4; i_4--)
    {
        x++;
        f=f-0.10;
    }
    for(float i_5 = f;0.01<=i_5; i_5--)
    {
       x++;
       f=f-0.01;
    }
    printf("I owe you %i coins", x);
    }   

I am using Turkish coins, there are 5 different types of Turkish coins and they have values as listed in the code, but my program seems to not count the coins that are worth 0.01 Liras. Here is what it looks like

jharvard@appliance (~/pset1): ./greedy
How much?4.59
I owe you 6 coins
sjharvard@appliance (~/pset1): ./greedy
How much?4.60
I owe you 6 coins
sjharvard@appliance (~/pset1): ./greedy
How much?4.50
I owe you 5 coins
1

The purpose of this pset is to demonstrate the flaws with storing floats in a computer. Simply put, fractional numbers (not whole numbers) are only close approximations. For example, if you store 4.2 as a float, it is actually stored as something like 4.199999999999837 (this isn't the exact number, but you get the idea.) If you print it out using %f, it will print as 4.2 because it is using default formatting and will round up. If, however, you were to print it out with formatting that goes out to all of the stored digits, you would see the actual stored number.

Now, when you have this and do a test like if (x >= 4.200), it would be false because x would be less than 4.2, although it is only by 0.00000000000xxx (or whatever the actual number). You might think the number is 4.2, but the computer says it is smaller when it does the comparison.

So that's what's going on. And just a heads up. While you can be applauded for doing it for another country's coins, check50 is totally unforgiving. It expects exact output, so to pass, you need to do it to U.S. standard coins and without any extra or missing prompts or whitespace.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

3
  • I see, although only way around this problem i see is changing for example for(float i_5 = f;0.01<=i_5; i_5--) to for(float i_5 = f;0.009<=i_5; i_5--) since the difference is after many zeroes. But i think there should be some better way of doing it, this seems kind of cheeky. Thanks for your help
    – Arda Cuce
    Apr 18 '16 at 20:16
  • Well, yes, there is another way. Figure out a way to convert the value in the float to an int that you can work with. Seems like it was discussed in either the lectures or the shorts.
    – Cliff B
    Apr 18 '16 at 20:23
  • Must have missed it, thanks again.
    – Arda Cuce
    Apr 18 '16 at 20:31

You must log in to answer this question.

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