+ 2
Solution for „Halloween Candy“ in C (Problem)
Hey guys. So I started C Programming for my university and tried to solve the quiz „Halloween Candy“. Now I have the problem, that my solution works for Test 1 and Test but dosen‘t work for the three hidden Testcases and I have no idea why. I included the Bibliothek <math.h> to use the command „round“. Any ideas why it isn‘t working with the other testcases I can‘t see? I wonder if I can just post my code here. It might spoiler some people.
8 odpowiedzi
+ 8
Actually round is defined in #include<math.h> header file so according to standard if you using any mathematics function then u need to include math.h header file else it will generate warnings like include proper header files .
But i guess instead of round function u need to use ceil ()
//ceil means "round up" to the nearest full value. So 200÷3 = 66.666 recurring.
//what ceil does is round it up to 67.
//The opposite of ceil is floor, which rounds down to the lowest full value
+ 5
Max I have visited many websites and seen that we need to use #include<math.h> when u using mathematics function ceil round and floor are defined in math.h its c header file and if u forget to include it compiler will give u warnings as Vasiliy told all these functions are in #include<stdio.h> but its good practice to include proper header file its depend on compiler may be some of compiler won't give u errors.
In cpp we need to write
#include<cmath.h>
see in 2.1 this warning says provide a declaration of ceil
+ 3
Max, ♨️A.S.♨️
This is what it means not to practice in "C" for a long time, but I remembered everything ☺️
warning: implicit function declaration 'ceil' [-Wimplicit-function-declaration]
7 | printf ("%.f", ceil(c));
| ^ ~~~
warning: incompatible implicit ceil built-in function declaration include '<math.h>' or provide a 'ceil' declaration already says that the 'ceil' function is built into #include<stdio.h>,
but it has several types:
"float, double, long double"
and you need to explicitly define what type is needed so that there is no ambiguity.
Example:
#include <stdio.h>
int main() {
double ceil(double x);
float x = 1.01;
printf("%.f",ceil(x)); // 2
return 0;
}
As a conclusion, the signatory organizations require:
float ceilf(float x);
float x = 1.01;
printf("%.f",ceilf(x)); // 2
+ 2
Max you can try .round() ceil () and floor() all are defined in math.h header file we can use according to the problem
+ 1
Vasiliy just tested it. It works without math.h, but after the correct rounded output a error message is shown.
./Playground/file0.c: In function 'main':
./Playground/file0.c:7:18: warning: implicit declaration of function 'ceil' [-Wimplicit-function-declaration]
7 | printf("%f \n", ceil(c));
| ^~~~
./Playground/file0.c:7:18: warning: incompatible implicit declaration of built-in function 'ceil'
./Playground/file0.c:2:1: note: include '<math.h>' or provide a declaration of 'ceil'
1 | #include <stdio.h>
+++ |+#include <math.h>
2 |
With #include <math.> the error disapears. 🤷♂️ as a beginner i have no idea if this error might cause problems in complex programs
0
Thank you for the answer. I defined math.h at the beginning of the program. Does ceil use the correct rounding rules? Or did i missunderstood the task of the quizz and it should always just round up? This might be actually ghe problem here XD
0
I tried it with ceil() that solved the task. Thank you for the help. I will keep these commands in mind for the future.
0
It is not necessary to used ceil with double or float. My code below
#include <stdio.h>
#include <math.h>
int main() {
int houses;
scanf("%d", &houses);
//your code goes here
int c =ceil((100.00/houses)*2);
printf("%d",(c) );
return 0;
}