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?
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 ...
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?
"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.
It gives you the remainder in an equation... 4/2=2, and 1%2 is the remainder of 1/2
ohhhhhhh. so thats all it does?
Yep. You'd use in a way like this:Originally posted by Neoground1
ohhhhhhh. so thats all it does?Code:int number; cin >> number; if( (number % 2) = 0) { // number even } else { // number not even }
Please direct all complaints regarding this post to the nearest brick wallHave a nice day.
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?
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..
mazo,
Substitute what you like for 'a' and 'b', but your equation works.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; }
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
Face Master... use == 0 instead of = 0Originally 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 }
Not absolutely sure... but shouldn't it have using namespace std;?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
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
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;"Not absolutely sure... but shouldn't it have using namespace std;?![]()
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
>> Face Master... use == 0 instead of = 0
Silly me...always do that when postin fast
Please direct all complaints regarding this post to the nearest brick wallHave a nice day.
Thanks, Hunter2...I think.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;"
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
Really? I thought that was just me, since everyone else uses iteven Mr. Stroustrup believes that "using namespace std;" is a "pollution" of the global namespace![]()
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.