0

I have problem with Holloween Candy challenge for c programming language. Can anyone help me?

This is my code: #include <stdio.h> #include <math.h> int main() { int houses; float dollar; scanf("%d", &houses); //your code goes here dollar = (float)200/houses; printf ("%.0f",dollar) ; return 0; } I just put 200 because it is shortcut of (2*100) as two represent the number of dollar and 100 to find percentage value. I already passes test case 1 and 2 but the rest, i don't know which part was wrong since its hidden test case.

15th May 2020, 8:49 AM
Radin Misbah
Radin Misbah - avatar
6 Respuestas
+ 2
The result has to be rounded up. #include <math.h> dollar = ceil(200.0 / houses);
15th May 2020, 9:02 AM
Manu_1-9-8-5
Manu_1-9-8-5 - avatar
+ 2
ceil() returns a double so you'll need to cast it to an int (int)ceil() https://www.tutorialspoint.com/c_standard_library/c_function_ceil.htm
15th May 2020, 10:09 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Its just by the definition how the output should be: Output Format A percentage value rounded up to the nearest whole number. So e.g. 12.1 should not be printed as 12, but 13.
15th May 2020, 12:12 PM
Manu_1-9-8-5
Manu_1-9-8-5 - avatar
0
Thank you for those who answer my question, finally i get it. However, why we can't use my method? Any further explanation?
15th May 2020, 12:08 PM
Radin Misbah
Radin Misbah - avatar
0
Ouhhh, i thought it should be round up like what we have learnt in school which is if 12.1 until 12.4 will be printed as 12 and if 12.5 until 12.9 will be printed as 13. Thank you for your very helpful and useful information 😊.
15th May 2020, 10:51 PM
Radin Misbah
Radin Misbah - avatar
0
Radin Misbah When the problem description asks you to round up, it means round up to the nearest higher integer. Therefore you need to use the ceil function. If it were to say round down then you need to round down to the nearest lower integer. Then you need to use the floor function. To round it to the nearest integer as you describe you can use the round function.
4th Nov 2020, 10:10 PM
Macross-Plus
Macross-Plus - avatar