Videos
I'm trying to write a function that (kind of) emulates the ceiling function in the math library, in a way that is more useful for my purposes. The code I've written is not working as expected. It should take two integer arguments, divide one by the other, and round up to the nearest integer no matter what the result is.
Ex: 2/2=1 --> answer is 1; 10/3=3.333... --> round up to 4.
This is my code:
int ceiling(int number, int value){
float a = number/value;
int b = number/value;
float c = a - b;
if(c!=0){
b=b+1;
}
return b;
}Using the examples above,
ceiling(2,2); would return 1, as expected.
ceiling(10,3); returns 3, when it should return 4.
My thought process is that if a is a float, then 10/3 should be 3.33333..., and if b is an int, then 10/3 should be 3. If c is a float, a-b should be 0.33333..., then since c!=0, ceiling should increment b and return that value. Where am I going wrong?



