## Writing a ceiling function in C

Posted on: May 9, 2018, by : xnlampSeedebugging question guidelinesfor more info.

The modulus operator (%) essentially gives you the remainder. That is, a % b will give you the remainder of dividing a by b (so 7 % 2 = 1, 10 % 4 = 2, etc.)

use the following search parameters to narrow your results:

Good Content:Any external resources linked to should be up-to-date and correct.

submitted4 years agobybaltimore94

include (or exclude) results marked as NSFW

Im 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 Ive 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.

When posting a question about code, you must include the following:

4 points5 points6 points4 years ago(3 children)

To get floating-point division, you need to cast at least one of the two variablesnumberandvaluetofloatbefore you do the division. For example,

advanced search: by author, subreddit…

ceiling(10,3); returns 3, when it should return 4.

0 points1 point2 points4 years ago*(1 child)

REDDIT and the ALIEN Logo are registered trademarks of reddit inc.

find submissions from example.com

Thanks very much for your response. Makes sense now. Im not 100% sure but I dont think for this assignment Im allowed to use libraries other than stdio.h and stdlib.h. I have the rest of my program working fine (I think), it was just this one part giving me trouble, causing the entire program not to work as it should.

The variablesnumberandvalueare bothints. Therefore the expressionnumber/valueis integer division: it is the division of twointvalues. This is integer division, and it returns an integer value, regardless of whether you subsequently decide to store that integer value in afloatvariable or anintvariable.

[S]0 points1 point2 points4 years ago(0 children)

No Complete Solutions:Do not give out complete solutions.Guidethe OP to the solution, butdo not solveit for them.

Ex: 2/2=1 — answer is 1; 10/3=3.333… — round up to 4.

For what its worth, why arent you just doing this?

find submissions in subreddit

Wow, I dont know why it never occurred to me that using modulus would work. I even thought about it and passed it off thinking there wasnt a way I could employ it usefully. I must not have been thinking clearly, I had just woken up. Oh well, my program is finished and working properly now.

Good Comments:Abusive, racist, or derogatory comments towards individuals or groups are not permitted.

No piracy:Do not ask for or post links to pirated or illegal material. We also dont allow suggestions of piracy.

Ninja edit: Now I also know what casting is and how to do it, so this worked out for me anyway.

No bot tutorials:Tutorials about creating reddit bots are not allowed on this subreddit. They historically create lots of unnecessary work for moderators of many different subreddits.

find submissions by username

Do not delete your posts!Your problem may be solved, but others who have similar problems could profit from the solution/discussion in the thread. Use thesolvedflair instead.

Rendered by PID 115265 on app-505 at 2018-05-09 10:29:52.416269+00:00 running 8b0f88c country code: CN.

If youre not familiar with theternary operator, the above code is equivalent to:

Use of this site constitutes acceptance of ourUser AgreementandPrivacy Policy. © 2018 reddit inc. All rights reserved.

[S]0 points1 point2 points4 years ago(2 children)

Here,numberis cast to afloatbefore the division. Because one of the operands of the division is a floating-point value now, the division is floating-point division, not integer division.

No Referral Links:Do not post referral links to Amazon or other sites.

search for text in self post contents

e.g.subreddit:aww site:imgur.com dog

For more details, see ourrules page. If you see any posts or comments violating these rules, please report them.

ceiling(2,2); would return 1, as expected.

No Rewards:You may not ask for or offer payment when giving or receiving help.

Many conceptual questions have already been asked and answered. Read ourFAQpage and search old posts before asking your question. If your question is similar to one in the FAQ, explain how its different.

A couple of other things, if you didnt know them already.

For what its worth, you can also utilize integer division in a ceiling function.

int ceiling(int n, int v) if(!n%v) // If v evenly divides into n, n/v is the same as ceil(n, v), return n/v; // so just return n/v else // Else, we know that integer division floors, so simply return (n/v) + 1; // return the floor plus one.

Writing a ceiling function in C(self.learnprogramming)

Seeconceptual questions guidelinesfor more info.

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?

I use the!n%v, which is basically a shorter way of writingn%v == 0. In C, anifstatement evaluates to true for any non-zero value, and false only for a zero value.