I want to find the remainder of the division between a and b, but without using the reminder operator a%b.
I thought to subtract b from a as long as a>b, that will give the remainder, but I don't know how to write it in code. Please help!
I want to find the remainder of the division between a and b, but without using the reminder operator a%b.
I thought to subtract b from a as long as a>b, that will give the remainder, but I don't know how to write it in code. Please help!
I suppose that you will be working with integers, in which case you could try and make use of integer division to compute the remainder, e.g., 7 / 3 = 2, 2 * 3 = 6, and 7 - 6 = 1, which is the remainder of the division.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
If a and b are both integral types (int, long, etc), use the expression a%b. The % operator is also known as modulo or remainder operator.
If a or b are floating point, then look up the fmod() function in <math.h>
thanks
without using modulo:
Code:int a, b, quotient, remainder; quotient = a/b; remainder = a - quotient*b;
Yeah, that was what I suggested in post #2, though I was hoping to leave the actual code as an exercise for the reader
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
O_oI thought the OP had given up and was just going to use % based on post #4, and this seemed more like an issue with the math itself than an issue with being able to implement the math as code.
Why then did you post code with no further explanation of the mathematics?
Seriously, if you thought the example laserlight gave was insufficient why jump strait to posting code?
*sigh*
I think we have too many babysitters here these days.
Soma
I thought that once the OP looked at the math (if the OP ever looks at this thread again), it would be obvious. The only further explanation would involve details such as if integer division rounds towards 0 (remainder has same sign as dividend), or round towards -∞ (remainder has same sign as divisor), which is probably beyond what is needed for this thread.
O_oI thought that once the OP looked at the math (if the OP ever looks at this thread again), it would be obvious.
You think the mathematics was the issue? You didn't think this was a code issue?
A descriptive example of the mathematics had already been posted; you only posted code for such mathematics; you didn't elaborate, refine, or otherwise add to the mathematics example without simply jumping strait to posting code.
So, you simply posted code because you thought having the code would make the code obvious?
Yes, I imagine that is true.
Have you ever heard: "Give a man a fish, you feed him for a day; teach a man to fish, you feed him for a lifetime."?
Or less "folksy": "It is more worthwhile to teach someone to do something, than to do something for them."?
The purpose of the comments laserlight made was to nudge the original poster in the right direction. You have only banged the original poster over the head with an answer.
If, by using the comments laserlight made, the original poster discovers the pattern of relationships between what is known and what is needed the individual will learn to guide themselves for such relationships as they find.
You have instead taught that you are willing to throw code at people thus doing their work for them.
Soma
Again, I thought the OP had simply given up. I would have no problem if a moderator wants to delete posts # 5 through #11 (this is post #11), so the thread is back to where it was the last time the OP looked at this thread, or just edit post #5 and replace it with "example code removed".
Last edited by rcgldr; 06-11-2013 at 05:20 PM.
The easy answer: use the ldiv function provided by the standard library.
ldiv - Stdlib.h - C - C++ Computing Reference with Worked Examples
Of course the point of this problem might be to learn how the modulo operator works, but if not....