%(Modulus)

This is a discussion on %(Modulus) within the C Programming forums, part of the General Programming Boards category; I'm a beginner in C and i've started to learn what the function %, modulus is and i'm confused with ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    70

    Exclamation %(Modulus)

    I'm a beginner in C and i've started to learn what the function %, modulus is and i'm confused with how to use it in a program, and what does it have to do with the random(0);
    function.If you could explain it to me it would be very helpful,thank you.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Mod gives you the remainder of a division. Say we get a random number:

    12345

    We run mod-100 on it. That means it gives us:

    w = 12345
    x = w / 100 (or 123)
    y = x * 100 (or 12300
    z = w - y (or 45)

    That's pretty much what mod does. I broke it up into small steps, but that's what it's doing. So, if you apply the same thing to any random number (replace w's value with whatever you want, and you'll still end up with the 10s and 1s spot of the number (the remainder of division by 100).


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

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    60
    It's kinda of calculating remainder, it's like 3 % 2 = 1; 100 % 10 = 0; 5 % 2 = 1; 19 % 4 = 3
    and it has nothing to do with random(), which is used to generate a random value ( it should be rand() in C, stdlib.h )

  4. #4
    VIM addict
    Join Date
    May 2009
    Location
    Chennai, India
    Posts
    43
    % is a modulus operator which falls under Arithmetic Operator category. This operator is used to find the remainder. Here is the simple example which uses modulus operator to find the given number is odd or even.

    Code:
    #include <stdio.h>
    
    int main()
    {
    int num=0;
    printf("Enter any number:");
    scanf("%d",&num);
    if( num % 2 == 0)
    printf("%d is a even number\n",num);
    else
    printf("%d is a odd number\n",num);
    return 0;
    }

  5. #5
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    % is used to find a remainder of integer division.

    It's used with rand() to give it an upper limit.
    Code:
    int rn = 1 + ( rand() % 100)    // rn will always be 1 - 100
    This is generally a Bad Idea. See here.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's not a bad idea if you don't care how super random it is. For most things anyone here is ever going to do, it's fine. As a matter of fact, the link you cite tells you as much. It helps if you actually read what you link to.


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

  7. #7
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by quzah View Post
    It's not a bad idea if you don't care how super random it is. For most things anyone here is ever going to do, it's fine. As a matter of fact, the link you cite tells you as much. It helps if you actually read what you link to.


    Quzah.
    I read what I link.

    I think that it is better to highlight a more sophisticated approach with some additional complexity, then to insist on a more simple but naive solution.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    70
    I'm still confused with the % part, like how do u read num % 2? and what does 2 have to do with it?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by cuo741 View Post
    I'm still confused with the % part, like how do u read num % 2? and what does 2 have to do with it?
    When dividing integers, C doesn't do decimal answers. 17 divided by 3 is 5 remainder 2. You get the 5 with / and the 2 with %.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    70
    Huh?
    Code:
    #include <stdio.h>
    
    int main()
    {
         int m,c;
         
         for(c=0;c<100;c++)
         {
               m = c % 10;
               printf("%d\t",m);
         }
         return(0);
    }
    could you explain this to me i dont get it

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by cuo741 View Post
    could you explain this to me i dont get it
    At this point, I'm pretty sure the answer is "no". Do you not know how division works?

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    70
    so for example, its like rand() % 6 means u want the remainder of rand() / 6?

  13. #13
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by cuo741 View Post
    so for example, its like rand() % 6 means u want the remainder of rand() / 6?
    Yes.

    Lets say we have a roll of fabric 13m long and we want to cut it into sheets 5m long.
    Code:
    int main(void) {
    
      int a, b, rem, quot;
    
      a = 13;   // Total length of fabric. 
      b = 5;    // We want the sheets to be 5m long.
    
      quot = a / b;   // I.e. how many 5m long sheets we can get?
      rem = a % b;    // I.e. how many meters of fabric we'd have
                      // left after we'd made as many 5m long sheets we could?
    
      printf("Sheets: %d \n", quot);
      printf("Remainder: %d \n", rem);
    
      return 0;
    }
    Write some code an experiment with the operator! There are many application for it.

  14. #14
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by msh View Post
    Yes.

    Lets say we have a roll of fabric 13m long and we want to cut it into sheets 5m long.
    Code:
    int main(void) {
    
      int a, b, rem, quot;
    
      a = 13;   // Total length of fabric. 
      b = 5;    // We want the sheets to be 5m long.
    
      quot = a / b;   // I.e. how many 5m long sheets we can get?
      rem = a % b;    // I.e. how many meters of fabric we'd have
                      // left after we'd made as many 5m long sheets we could?
    
      printf("Sheets: %d \n", quot);
      printf("Remainder: %d \n", rem);
    
      return 0;
    }
    Write some code an experiment with the operator! There are many application for it.
    I like the fabric example good job.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    70
    Did i use it right?


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    { 
        int remainder;
        float hat,hat1;
        
        srand((unsigned)time(NULL));
        
        hat=rand(); 
        
        hat1=rand() % 5;
        
        
        
        printf("The remainder of %.2f divided by 5 = %.2f\n",hat,hat1);
        
        	  
        system("PAUSE");
        return(0);
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Tags for this Thread


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