Little bit confused on the Mod Operator

This is a discussion on Little bit confused on the Mod Operator within the C Programming forums, part of the General Programming Boards category; Here's a sample from my review: 7) How many times does the letter A print out in the following segment ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    32

    Question Little bit confused on the Mod Operator

    Here's a sample from my review:



    7) How many times does the letter A print out in the following segment of code?
    Code:
    int i=0;
    while (i < 20) {
      i++;
      if (i%5 > 2) {
        continue;
      }
      printf(“A”);
    }
    A) 10 B) 12 C) 15 D) 20 E) NOTA




    The answer is b, but I dont understand the if statement.
    For instance, I do understand if something like 12%5, which is 2 because 5 can only go up to 10, so the remainder is 2.
    But when it's something like 19%200, it throws me off

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    If the remainder of i / 5 is > 2 then don't print an "A"
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1234567890
    YYNNYYYNNY = 6

    Now multiply that by 2.



    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    But when it's something like 19%200, it throws me off
    19 / 200 is zero, remainder 19.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,269
    Or to be really picky, that segment of code wont compile (as posted) because it contains smart-quotes around the "A", rather than regular ones.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    32
    Quote Originally Posted by Dino View Post
    19 / 200 is zero, remainder 19.
    So if thats the case, then wouldnt the program read like this:

    Code:
    if (i%5 > 2)
    1=5
    2=5
    3=5
    4=5
    5=0
    6=1
    7=2
    8=3
    9=4
    10=0

    so i= 1, 2, 3, 4, 8 and 9 is true since it is bigger than two

    am I understanding this correctly?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, look back at my earlier post:

    1%5 = 1 > 2 = F
    2%5 = 2 > 2 = F
    3%5 = 3 > 2 = T
    4%5 = 4 > 2 = T
    5%5 = 0 > 2 = F

    The % operator is basic division, before they taught you about decimals.

    1 / 5 = 0 R 1
    2 / 5 = 0 R 2
    3 / 5 = 0 R 3
    4 / 5 = 0 R 4
    5 / 5 = 1 R 0

    So you look at the remainder, and see if that is bigger than 2.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,012
    Quote Originally Posted by Neotriz View Post
    So if thats the case, then wouldnt the program read like this:

    Code:
    if (i%5 > 2)
    1=5
    2=5
    3=5
    4=5
    5=0
    6=1
    7=2
    8=3
    9=4
    10=0

    so i= 1, 2, 3, 4, 8 and 9 is true since it is bigger than two

    am I understanding this correctly?
    No that's not quite right. It should be:

    0≡0
    1≡1
    2≡2
    3≡3
    4≡4
    5≡0
    6≡1
    7≡2
    8≡3
    9≡4
    10≡0

    All of those are modulo five of course. Note how the right is never 5 or greater.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    1 % 5 = 1
    2 % 5 = 2
    3 % 5 = 3 First match
    4 % 5 = 4. 2nd match
    5 % 5 = 0
    6 % 5 = 1
    7 % 5 = 2
    8 % 5 = 3 3rd match

    and so on

    and so on
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    32
    Thank you! I was actually looking the other way around

    Thanks alot, that clears up

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why can't my perceptron learn correctly?
    By yann in forum C Programming
    Replies: 25
    Last Post: 10-15-2010, 12:26 AM
  2. bit value generation doubt
    By roaan in forum C Programming
    Replies: 2
    Last Post: 08-07-2009, 02:23 PM
  3. a bit confused with char string~
    By black in forum C++ Programming
    Replies: 3
    Last Post: 08-05-2002, 12:37 PM
  4. A little bit confused...pls help.
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-06-2001, 11:45 PM
  5. int vs BIG INT
    By rumi_red in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2001, 03:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21