Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

JotaKa

modulus operator

This topic is 5318 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

I''m trying to make a program that prints all the numbers between one and ten that are divisible by two. I imagine this can be accomplished using the modulus operator? any help with my code would be appreciated. // All numbers divisible by 2 up until ten #include <math.h> #include <iostream.h> main() { for (int i = 0; i <=10; i++) { cout<<10 % i; } return 0; }

Share this post


Link to post
Share on other sites
Advertisement
If you want numbers that are divisible by 2, then the number 2 should be involved somewhere close, oh so very close, to the modulus operator, don't you think ? After all, what is the definition of "x is divisible by y" ? What is the definition of the modulus operator, what is "x % y" ?


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan (C programming language co-inventor)


[edited by - Fruny on February 27, 2004 4:56:53 PM]

Share this post


Link to post
Share on other sites
hmmm, ya think i'd remeber to put 2 in the equation???? yeah....I have a tendency to just assume the computer know's what I want it to do :-) i meant to say that i don't want a remainder, just what's evenly divisible by two, my math terminology is failing me... thanks for the fast replies, it's greatly appreciated.

[edited by - jotaka on February 27, 2004 5:07:42 PM]

Share this post


Link to post
Share on other sites
This is just a brain dump of sorts:
  1. The concept of divisibility can be inverted to yield "multiple." ie, you can achieve the same result by simply computing the multiples of your target variable (2 in this case) that are less than your target limit (10 in this case). Unless you''re just fooling around with the modulus operator.

  2. For large collections of divisibility (such as the computation of primes), using methods similar to the Sieve of Eratosthenes is better: Create an indexable bitset (std::bitset or std::bitvector) and zero out indexes that are multiples of your base value. In the case of primes you can stop once you encounter an already zeroed multiple, since all subsequent multiples will have been zeroed as well. Then for whatever limit you have, iterate through your bitset and print the indexes (bitplaces) whose values are non-zero.

  3. In the case of your program, what it currently does is print the remaind of dividing 10 by all numbers less than it (unfortunately, including zero - which yields a software interrupt). What you want is to print the values up to 10 whose remainder is zero; to get the quotient you''ll want to employ C integer division.

Hope that helped.

Share this post


Link to post
Share on other sites
quote:
Original post by JotaKa
i meant to say that i don''t want a remainder, just what''s evenly divisible by two, my math terminology is failing me...
Quotient.
for( int n = 1; n <= 10; ++n )
if( n % 2 == 0 ) cout << n << endl;

Share this post


Link to post
Share on other sites
Divisible by two?

for (int i=0; i<100; i++) if (!(i&1)) printf("%d\n", i);


EDIT: please note 0 is not divisible by two as 0 is not in 1, 2, 3, ... (N);

for (int i=1; i<100; i++) if (!(i&1)) printf("%d\n", i);


[edited by - Kurioes on February 27, 2004 5:42:20 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Kurioes
EDIT: please note 0 is not divisible by two as 0 is not in 1, 2, 3, ... (N);



Zero is divisible by 2.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan (C programming language co-inventor)

Share this post


Link to post
Share on other sites
thanks for all the replies everyone, I was having a brainfart, logic centers of the brain rendered inoperable momentarily. Shortly after i created the intended program. I was trying to implement the modulus operator, however I do like the various other ways of coming to my desired solution so thanks again everyone for the help

Share this post


Link to post
Share on other sites

O.K. Think about even and odd numbers:
1)Even numbers yield a particular number when % by a
CERTAIN number.
2)Odd numbers yield a particular number (which is different
from the even number''s result) when % by the same CERTAIN
number above.

Share this post


Link to post
Share on other sites

  • 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!