Reputation: 5302
Sorry for this newbie question, but I can't find on google what I need to know.
I understand return, but don't understand this... What does it mean this?
return (tail+1)%N == head%N;
Thanks a lot for patience.
Upvotes: 3
Views: 175
Reputation: 403
it returns true if remainder of the division for tail + 1 and head is the same
for example if tail is 2, head is 1 and N is 2
(tail + 1) % N is 1
head % N is 1 too
so whole expression returns true
Upvotes: 2
Reputation: 3905
Short Answer:
Because of the ==
operator your function will return a bool
, meaning it can only be true
or false
. An equivalent would be something like:
return 5 == 4;
which would return false
since 5 is not equal to 4.
Long Answer:
Instead of writing this in a single line you could split it up into more lines of code. Let's just assume that tail
, head
and N
are integer values, then you could write it like this:
int x, y;
x = (tail+1)%N;
y = head%N;
if ( x == y )
{
return true;
}
else
{
return false;
}
Now in this code there may be also that %
confuses you a bit. The %
is called the Modulus Operator
and can give you the remainder of arithmetic operations. In a simple example this would mean:
10 % 3 = 1
because 10/3
is 3 with a remainder of 1. So to make it more clear let's just make another example with your specific problem:
Lets just assume that tail=10
,head=6
and N=2
. Then you would get something like this:
x = (10+1)%2
x = 11 % 2
x = 1
y = 6 % 2
y = 0
y != x
This would return false
cause x
and y
are not equal. ( If you would run your code with the given example values )
To learn more about Modulus
you can look here, or just on any other basic C++ Tutorial.
Upvotes: 2
Reputation: 223207
this
(tail+1)%N == head%N
returns a boolean value, either true or false. This statement means that after adding 1 to trail (trail + 1) and the remainder obtained after division with N is equal to remainder of head divided with N. % is used for division with remainder
(%). Modulo is the operation that gives the remainder of a division of two values.
Check this link for c++ operators : http://www.cplusplus.com/doc/tutorial/operators/
Upvotes: 3
Reputation: 409166
It evaluates the expression, and return the result. In this case it's two modulo operations that are compared, and the result is either true
or false
which will be returned.
Upvotes: 2
Reputation: 4698
you're returning a boolean value. The value represents whether or not the remainder of (tail+1) divided by N is the same as that of head.
Upvotes: 2
Reputation: 258558
It returns true
or false
, depending on whether the expression is true or not.
It's the same as:
if ( (tail+1)%N == head%N )
return true;
else
return false;
Upvotes: 5