+ 3

[Solved]Debug my 'C' code and explain the cause of WA

This code works almost every input but doesn't work for particular input like '576.43'.Works properly also for 476.43 & 576.73.Facing problem at line 43 or nearby,I guess. https://code.sololearn.com/c07J5iHqb51p/?ref=app

19th Mar 2022, 6:19 AM
KAZI FAISAL MAHMUD
KAZI FAISAL MAHMUD - avatar
11 Answers
+ 4
KAZI FAISAL MAHMUD you are very much correct, that didn't work. I've been playing with it, I've tried multiplying both taka and N by 100 but it was still giving the rounding error. I've just found if we use: coin=(1000*N-1000*taka)/10; Then the rounding error is gone. Then you have to adjust all of your coin calculations to work with numbers 1 < coin < 100 instead of 0.01 <coin < 1.00 https://code.sololearn.com/cmE5lwgFujMk/?ref=app
19th Mar 2022, 8:01 AM
HungryTradie
HungryTradie - avatar
+ 4
HungryTradie then input might better be a string and splittet at decimal point?
19th Mar 2022, 8:11 AM
Oma Falk
Oma Falk - avatar
+ 3
G'day KAZI FAISAL MAHMUD well done on making it work that way. I think your error is in dividing by 0.01. The C language shows us decimal numbers but actually uses binary to store and calculate. Sometimes a division operation has a small error when converting from binary to decimal. I have had success moving my calculations to become integers, eg coins *100. Your math would then be zero_one=(coin*100-(half*50+one_forth*25+one_tenth*10+5*zero_five))/1;
19th Mar 2022, 7:26 AM
HungryTradie
HungryTradie - avatar
+ 2
HungryTradie that was my idea too. 👍🏻
19th Mar 2022, 7:44 AM
Oma Falk
Oma Falk - avatar
+ 2
HungryTradie Thank you.You did well really.
19th Mar 2022, 8:09 AM
KAZI FAISAL MAHMUD
KAZI FAISAL MAHMUD - avatar
+ 2
Thanks mate, it was some good learning for me too! Would you be a champion of this SoloLearn Q&A forum? If your question is solved, would you edit your original question to start with [Solved]
19th Mar 2022, 8:12 AM
HungryTradie
HungryTradie - avatar
+ 1
HungryTradie Why doesn't it work when I am doing like below zero_one=(coin-(half*0.5+one_forth*0.25+one_tenth*0.1+0.05*zero_five))*100/1 Have I to use one more ()? Anyway,it doesn't work on your way too.
19th Mar 2022, 7:51 AM
KAZI FAISAL MAHMUD
KAZI FAISAL MAHMUD - avatar
+ 1
HungryTradie Which line are you mentioned to change by 'coin=(1000*N-1000*taka)/10;'
19th Mar 2022, 8:07 AM
KAZI FAISAL MAHMUD
KAZI FAISAL MAHMUD - avatar
+ 1
Ok mate
19th Mar 2022, 8:20 AM
KAZI FAISAL MAHMUD
KAZI FAISAL MAHMUD - avatar
+ 1
When doing monetary calculations try to do it in integers. You can adjust the scanf format to split on the decimal point and read directly into taka and coin like this: scanf("%d.%d",&taka, &coin); Now both are whole numbers. Also see how I reduced repeated calculations. The result was correct for 576.43. int taka, coin; int one,two,five,ten,tweenty,fifty,hundred,half,one_forth,one_tenth,zero_five,zero_one; scanf("%d.%d",&taka, &coin); hundred=taka/100; taka %= 100; fifty=taka/50; taka %= 50; tweenty=taka/20; taka %= 20; ten=taka/10; taka %= 10; five=taka/5; taka %= 5; two=taka/2; taka %= 2; one=taka; taka = 0; half=coin/50; coin %= 50; one_forth=coin/25; coin %= 25; one_tenth=coin/10; coin %= 10; zero_five=coin/5; coin %= 5; zero_one=coin; coin = 0;
19th Mar 2022, 9:03 AM
Brian
Brian - avatar
0
Yeah Oma Falk It is something before those divisions. When casting taka to (int) it causes the error in the value, subtracting taka from N and then multiplying, I've seen it change from 43 to 42 (or 38 to 37, 28 to 27, 3 to 2).
19th Mar 2022, 7:49 AM
HungryTradie
HungryTradie - avatar