Sign in to follow this  
neshtak

Need help understanding Modulus

Recommended Posts

Someone explain how it works. The only thing I know is that modulus arithmetic operator is represented as "%"
Quote:
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { srand(time(0)); int randomNumber = rand(); int die = (randomNumber % 6) + 1; cout << "You rolled a " << die << endl; cin.ignore(cin.rdbuf()->in_avail() + 1); return 0; }

Share this post


Link to post
Share on other sites
It gives the remainder in a division of two (positive - I'm not sure that it's not implementation-defined behaviour for negative) integers:

3 % 2 = 1
4 % 2 = 0

It's handy for looping around at a certain value and for detecting whether a number is exactly divisible by something, as well as having utility in various areas (such as hashing and cryptography).

Share this post


Link to post
Share on other sites
Quote:
Original post by return0
It returns the remainder of dividing two integers. In that example it's bringing a random number into the range 0 to 5.


The modulus operator is a binary operator used to give you the remainder of dividing two operands. It's called a binary operator because it takes two operands and returns the remainder. For example if you have 23%3,the remainder will be 2, because 23/3 is 7, and 3*7 = 21, and take that and subtract it from 23 and you get a remainder of 2.



Share this post


Link to post
Share on other sites
Yep, I think you got it. Good job.

Here is a link telling you a little more about the modulus operator. I think you should just read the first paragraph. Below the first paragraph things tend to get cryptic. Happy coding and good luck.
Here is the link:
http://en.wikipedia.org/wiki/Modulo_operation

Since you are learning about the modulus operator, I think you should also be aware of order of operations. I tried finding a link that discusses order of operation but I could not find one. I'm going to look in one of my old c++ book to see if I can find something that talks about order of operation.

[Edited by - boolean010 on July 24, 2008 6:38:01 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by boolean010
Quote:
Original post by return0
It returns the remainder of dividing two integers. In that example it's bringing a random number into the range 0 to 5.


The modulus operator is a binary operator used to give you the remainder of dividing two operands. It's called a binary operator because it takes two operands and returns the remainder. For example if you have 23%3,the remainder will be 2, because 23/3 is 7, and 3*7 = 21, and take that and subtract it from 23 and you get a remainder of 2.




Why quote me if you're just going to repeat what I said in a more convoluted way?

Share this post


Link to post
Share on other sites
Quote:
Original post by TheUnbeliever
It gives the remainder in a division of two (positive - I'm not sure that it's not implementation-defined behaviour for negative) integers:

3 % 2 = 1
4 % 2 = 0

It's handy for looping around at a certain value and for detecting whether a number is exactly divisible by something, as well as having utility in various areas (such as hashing and cryptography).

It also comes in handy when you want to limit the number of values per line you want to output for example:
[source lang = "cpp"]
// output 5 primes per line
for(int i = 0; i < max; i++)
{
if(i % 5 == 0)
cout << endl;
cout << setw(10) << *(pprime + i);
}


You can change the 5 to a smaller or bigger number depending on how many numbers you want per line.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this