0

I faced a problem when executing greedy.c. I think every thing is correct (or probably not), but every time I put $4.2 as the input the program outputs 22 coins, which should be 18 ( 16 x 25c + 2 x 10c). S o a total of 18 coins.

After digging around I found that when I input 4.2, the round command rounds that number to 4.19 (16x25c + 1x10c + 1x5c + 4x1c). So I get the number of coins required as 22 coins.

While all inputs work fine both prerounded numbers and numbers that need rounding. But I only face this 4.2. I check50 only 4.2 created a problem.

1
  • I've also got this problem. Did you solve it yet? – Yujia Lv Feb 25 '15 at 14:30
1

Did you try using round command after multiplying 4.2 by 100? uma1966

0

The problem is with the input imprecision. I had the same problem and worked it out by considering 1 penny anything >= 0.006, 1 nickel anything >= 0.046, 1 dime anything >= 0.096, and a quarter anything >= 0.246.

You see, when you input 4.20/4.2 in the terminal, it becomes 4.1999999... because of the imprecision of float/double in the C language. So if you consider 0.096 as 0.10 cents you'll have 4.20 instead of 4.19.

1
  • It's a workaround, but misses the point of the pset. There are a lot of practical applications where this approach simply wouldn't be reliable. It is better to find a solution that deals directly with the imprecision issues reliably and efficiently. I'd go into greater detail, but that would be giving away too many clues on the solution. – Cliff B Oct 21 '15 at 2:57

You must log in to answer this question.

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