what does mod (%) do?

This is a discussion on what does mod (%) do? within the C++ Programming forums, part of the General Programming Boards category; I know that this is a stupid question, but I was asleep in class when the teacher explaned it and ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    73

    Question what does mod (%) do?

    I know that this is a stupid question, but I was asleep in class when the teacher explaned it and my book doesn't give a clear cut answer. So can anyone help me?

  2. #2
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    "a % b" will give you the remainder when a is divided by b. Mod is very useful, especially when you want to figure out whether or not a number is even.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    It gives you the remainder in an equation... 4/2=2, and 1%2 is the remainder of 1/2

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    73
    ohhhhhhh. so thats all it does?

  5. #5
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    Originally posted by Neoground1
    ohhhhhhh. so thats all it does?
    Yep. You'd use in a way like this:
    Code:
    int number;
    cin >> number;
    
    if( (number % 2) = 0)
    {
        // number even
    }
    else
    {
        // number not even
    }

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    73
    I was also wondering if there is a way to skip a line of data when you are reading it from a file. This is what im tring to do. I am reading in data from a file. The file has 4 lines of data (40 letter grades and the names of the student after the data) and im trying to skip to the next line after i read 30 charactors. How can i do that?

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    29

    % and / operators

    Hmm.. for positive integers it's working really, but what if some of given operands is negative? I think that then % operator returns the value, for which this is true: (a/b)*b + (a%b) = a. (maybe I should have said that the equality holds, but I'm not good in english.. what do you think?)
    But I'm not quite sure what is a/b doing when one of its operands is less than 0..

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    mazo,

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <cmath>
    
    inline void pause() { system("PAUSE"); }
    
    int main(void)
    {
    int a, b, c;
    
    a = 35;
    b = -7;
    
    c = (a/b)*b + (a%b);
    
    std::cout << "a = " << a << std::endl << std::endl;
    std::cout << "b = " << b << std::endl << std::endl;
    std::cout << "(a/b) = " << (a/b) << std::endl << std::endl;
    std::cout << "(a/b)*b = " << (a/b)*b << std::endl << std::endl;
    std::cout << "(a%b) = " << (a%b) << std::endl << std::endl;
    std::cout << "(a/b)*b + (a%b) = " << c << std::endl << std::endl;
    
    pause();
    return 0;
    }
    Substitute what you like for 'a' and 'b', but your equation works.

    By the way, your English is quite good. My Slovak is very, very poor. In fact, I know only one word: mazo.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Originally posted by face_master
    Yep. You'd use in a way like this:
    Code:
    int number;
    cin >> number;
    
    if( (number % 2) = 0)
    {
        // number even
    }
    else
    {
        // number not even
    }
    Face Master... use == 0 instead of = 0

  10. #10
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Originally posted by skipper
    mazo,

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <cmath>
    
    inline void pause() { system("PAUSE"); }
    
    int main(void)
    {
    //code
    std::cout << "a = " << a << std::endl << std::endl;
    //more code

    -Skipper
    Not absolutely sure... but shouldn't it have using namespace std;?

  11. #11
    Registered User
    Join Date
    Jun 2002
    Posts
    79
    Note, the % operator can be used only with integers. If you want to perform the same operation you will have to use fmod or modf, both functions defined in math.h.

    Compiler:Turbo C++ 1.01

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Not absolutely sure... but shouldn't it have using namespace std;?
    No, that's the lazy man's way of doing things. The true programmer will do things the hard way and type out std:: in front of everything, instead of using the new-fangled dad-blasted "using namespace std;"
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #13
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    >> Face Master... use == 0 instead of = 0

    Silly me...always do that when postin fast

  14. #14
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    No, that's the lazy man's way of doing things. The true programmer will do things the hard way and type out std:: in front of everything, instead of using the new-fangled dad-blasted "using namespace std;"
    Thanks, Hunter2...I think.

    Actually, given that even Mr. Stroustrup believes that "using namespace std;" is a "pollution" of the global namespace, I tend to use std:: as much as is feasible.

    I also try to emulate (note the word "try") the methods of the experienced and knowledgeable programmers on the Board, as well.

    Probably a good idea to research the topic and decide for yourself.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  15. #15
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    even Mr. Stroustrup believes that "using namespace std;" is a "pollution" of the global namespace
    Really? I thought that was just me, since everyone else uses it
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

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

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