so this is my code for greedy. When I run it, the amount of necessary coins is slightly imprecise ( when I modify it to a float value, I see just a clean 3.00000 * 10 for example with a roundf for change). For example, when I input 0.41 it shows 3 or 5 to 22 coins or 10 to 42 coins, never actually getting the precise amount of coins needed even if the aforementioned modification not in this topic shows a clean floating value.

Please help !

Removed code, problem solved.


The point of this pset is to understand how floats are stored in a computer and the ramifications of that imprecise storage. As written, your program depends on exact representation of numbers in the computer, but the very imprecision of those floats is causing your difficulties.

What is wrong with this code for greedy (pset1)? goes into more detail, and you should consider reviewing the class material on floats.

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

  • Thanks for your answer. However I don't know how my conversion is wrong. Please take a look at this: cents = (int) roundf(dollar * 100); with dollar being a float and cents an integer. I've edited my question accordingly to the new change.
    – Gabriel
    Nov 13 '15 at 23:58

You must log in to answer this question.

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