So, I understand the point of the exercise is to show how imprecisely the floating points numbers are stored on the computer, I believe I have changed the floats to integers, but when I compile and run the code this happens:

1. If I store an input of 1:

jharvard@appliance (~/Dropbox/CS50/pset1/common): ./greedy Type input: 1

You gave the customer 7 coins! How greedy of you!

Here the obvious result would be 4 quarters.

2. The CHECK50 results:

jharvard@appliance (~/Dropbox/CS50/pset1/common): check50
2015.fall.pset1.greedy greedy.c 
:) greedy.c exists 
:) greedy.c compiles 
:( input of 0.41 yields output of 4    \ expected output, but not "\nYou gave the customer 4 coins! How gr..." 
:( input of 0.01 yields output of 1    \ expected output, but not "\nYou gave the customer 4 coins! How gr..." 
:( input of 0.15 yields output of 2    \ expected output, but not "\nYou gave the customer 4 coins! How gr..."
:( input of 1.6 yields output of 7     \ expected output, but not "\nYou gave the customer 9 coins! How gr..." 
:( input of 23 yieldsoutput of 92      \ expected output, but not "\nYou gave the customer 95 coins! How g..." 
:( input of 4.2 yields output of 18    \ expected output, but not "\nYou gave the customer 20 coins! How g..." 
:)rejects a negative input like -.1 :) rejects a non-numeric input of "foo" 
:) rejects a non-numeric input of ""

Here is the code...

#include <stdio.h>
#include <cs50.h>
#include <math.h>

/* Change input variable */
float change;

/* Value of each coin */
int quarter = 25, dime = 10, nickle = 5, pennie = 1;

/* Amount of coins for final printf */
int coins_amount = 0;

/* *** PROGRAM STARTS *** */
int main (void)
/* Asks for user input.*/
        printf ("Type input: ");
        change = GetFloat();

        /* Rounding the float change value.*/    
        change = (int)round(change *100);    

    }while (change <= 0);

        /* Beggining of the loop*/
        while (change > 0)
        /* If quarter is smaller than the change do this...*/

                change = (change - quarter);
                coins_amount = (coins_amount + 1);

            }while (quarter <= change);

         /* If dime is smaller than the change do this...*/

                change = (change - dime);
                coins_amount = (coins_amount + 1);

            }while (dime <= change);

         /* If nickle is smaller than the change do this...*/

                change = (change - nickle);
                coins_amount = (coins_amount + 1);

            }while (nickle <= change);

         /* If pennie is smaller than the change do this...*/

                change = (change - pennie);
                coins_amount = (coins_amount + 1);

            }while (pennie <= change);

        printf ("\nYou gave the customer %d coins! How greedy of you!\n", coins_amount);

I know is not elegant at all, but I've been breaking my head to do this. What is wrong with it? And where could I found more material to learn more about this floating errors?

1 Answer 1


Three things jump out at me. First, follow the instructions. Second, change is still a float. Third, you're using do/while loops.

Follow the instructions. The first hidden lesson is to do exactly what the program specification says. This is especially important in team programming where the pieces all have to fit together later. In this case, your program exceeds the spec by adding extra commentary around the number of coins that is supposed to be returned. check50 expects exact output, so any extra or missing comments, data, line feeds, or even white space will trigger a fail, as shown in your results where the number may be right, but you still have those extra comments.

Still a float.
Even though you convert the value returned from GetFloat() to cents to an int, you store it right back in a float. While this may actually work, it is dangerous.

DO/WHILE loops: In your loops, you are using do/while loops instead of while loops. A do/while loop will ALWAYS execute at least once. So, in this case, let's say that the original amount is $0.01. No matter what, you're going to have at least 1 quarter, 1 dime, 1 nickel and 1 penny. For any amount, you'll get at least those 4 coins, plus whatever other coins happen to get calculated in.

There may be other issues, but these are big enough that they need to be addressed first. Changes may fix other issues, if any, or may introduce more. This will get you started though.

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

  • Thanks for the help @Cliff-B, I've changed the DO-WHILE loop for a While loop and then made the program print only the ("%d\n") and... The Check50 aproved it. But I'm still wondering what do you mean by "storing it as a float" yet. And why is it dangerous? Mar 26, 2016 at 1:26
  • Well, you put the resut back into change, which is still of type float. Instead, you could have stored it in a different var of type int.
    – Cliff B
    Mar 26, 2016 at 1:27
  • I see now. Just made the changes! I'll study more about Data Types and Float or Int storing so I don't make more of those mistakes! Mar 26, 2016 at 1:29

You must log in to answer this question.

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