Jump to content
  • Advertisement
Sign in to follow this  
Samaanc

A question about modulus?

This topic is 5072 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ok i know what modulus does, but i don't know how to use it to my advange so here is my question #include <iostream> using namespace std; int main() { unsigned short small; unsigned long large; unsigned long skip; unsigned long target; const unsigned short MAXSMALL=65535; cout << "Enter a small number: "; cin >> small; cout << "Enter a large number: "; cin >> large; cout << "Enter a skip number: "; cin >> skip; cout << "Enter a target number: "; cin >> target; cout << endl; while (small < large && large > 0 && small < MAXSMALL) { small++; // incremeting small if (small % skip == 0) // my question is what does this do? { cout << "skipping on " << small << endl; continue; } if (large == target) { cout << "Target reached!\n"; break; } large -= 2; } cout << "Small : " << small << " Large: " << large << endl; system("PAUSE"); return 0; }

Share this post


Link to post
Share on other sites
Advertisement
Modulus calculates the remainder of a division.

Examples:
4 % 3 = 1. (4 / 3 = 1, 1 left over)
4 % 2 = 0. (4 / 2 = 2, 0 left over)
4 % 1 = 0. (4 / 1 = 4, 0 left over)

So to answer your question:

if (small % skip == 0) // my question is what does this do?
{
cout << "skipping on " << small << endl;
continue;
}


Think of it in terms of actual numbers.

if (4 % 4 == 0) // my question is what does this do?
{
cout << "skipping on " << small << endl;
continue;
}


So it'll skip the #4, because 4 % 4 = 0 (4 / 4 = 1, 0 left over).

Share this post


Link to post
Share on other sites
Quote:
Original post by Ekim_Gram
You posted the code but we need a more detailed question about what your asking.


ok, im wondering on the line
if (small % skip == 0) // my question is what does this do?
{
cout << "skipping on " << small << endl;
continue;
}
what does (small % skip == 0); do here in detail and why does the % operator does in full detail?


oh dang, ok i see now
thank you! :)

Share this post


Link to post
Share on other sites
Oops, didn't see the comment in your code. That's basically saying if there is no remainder when you divide the two numbers, it performs the instructions given to it.

Share this post


Link to post
Share on other sites
Quote:

Think of it in terms of actual numbers.


This is good advice. For example if you want to know what numbers are selected for (a % b == 0), then it will be those a equal to:

0, b, 2*b, 3*b, 4*b, ...

Similarly if you have (a % b == 1), you get:

1, b+1, (2*b)+1, (3*b)+1, ...

HTH,
Jim.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!