Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cybrscot

macrumors 6502
Original poster
Dec 7, 2010
282
0
Somewhere in Southeast Asia
question d in my book: i, j, k are int variables

i = 3; j = 4; k = 5;
printf ("%d", i % j + i < k) ;

The program output is 0. However, the expression is left associative, and when I do it in my head, I think that 3 % 4 is 0, then 0 + 3 is 3, and lastly, 3 is < 5,

so, since 3 < 5, then i < k, which means it's a true statement and the output should therefore be 1 (true), and not 0 (false)

What am I getting wrong when I think it through?



I wrote:

Code:
#include <stdio.h>

main ()

{

	int i, j, k ;
	
	i = 3 ;
	j = 4 ;
	k = 5 ;
	
	printf ("%d\n", i % j + i < k) ;
	
	return 0 ;
	
}
 
Based on the order of operations, this could be parenthesized like this:
((3 % 4) + 3) < 5
(3 + 3) < 5
6 < 5
0

I think you just missed how mod works. How many times can 4 go into 3? 0. So 3-0*4 leaves a remainder of 3.

-Lee
 
Based on the order of operations, this could be parenthesized like this:
((3 % 4) + 3) < 5
(3 + 3) < 5
6 < 5
0

I think you just missed how mod works. How many times can 4 go into 3? 0. So 3-0*4 leaves a remainder of 3.

-Lee

Thanks Lee, from looking at your example, I can see that if the 3 % 4 equals a remainder of 0, then we stick with the original 3, then add it to the next 3, equaling 6, and no 6 < 5, so 0 false.

But I don't understand part of your explanation where after you say that 4 goes into 3 = 0, you then say 3-0*4 leaves a remainder of 3. Where are you getting the - operator from? It's not in the expression, also, there is no *4 in the expression. Where are these coming from?

The expression only has 3 % 4, which is 0. So that part in the order of operations is complete, right? Then since the remainder is 0 don't we add that to 3 as the next part of the order of ops?

I can see in your example that 3 % 4, while it is 0 remainder.....Do we just stick with the first integer, which is 3 in this case, when the remainer is 0?
 
Thanks Lee, from looking at your example, I can see that if the 3 % 4 equals a remainder of 0, then we stick with the original 3, then add it to the next 3, equaling 6, and no 6 < 5, so 0 false.

But I don't understand part of your explanation where after you say that 4 goes into 3 = 0, you then say 3-0*4 leaves a remainder of 3. Where are you getting the - operator from? It's not in the expression, also, there is no *4 in the expression. Where are these coming from?

The expression only has 3 % 4, which is 0. So that part in the order of operations is complete, right? Then since the remainder is 0 don't we add that to 3 as the next part of the order of ops?

I can see in your example that 3 % 4, while it is 0 remainder.....Do we just stick with the first integer, which is 3 in this case, when the remainer is 0?

Hrm.

I was trying to express what mod means in a mathematical sense. You take the left operand and divide by the right operand. In this case, 3 / 4. 4 cannot fit into 3 any times, so the result of the division is 0. The subtraction bit was just finding the remainder. So since 4 goes into 3 0 times, you can find out how much to remove from 3 to find the remainder by multiplying 4 by the times it goes into 3, which is 0. I was just trying (unsuccessfully) to step through the "long" way one could calculate a remainder of a division.

You keep saying that 3 % 4 has a remainder of 0. That is wrong. The result of the division of 3 / 4 is 0, but modulus is the remainder of that operation. If the right operand of % is larger than the left operand, than yes, this will evaluate to the left operand. If the left operand is larger, % evaluates to the remainder of the division of the left operand by the right operand.

3 % 4 is first in the order of operations, but this evaluates to 3, not 0.

To use another example:
13 % 5
13 / 5 = 2
13 - (5 * 2)
13 - 10
3

3 is the result of this modulus operation, because it is the remainder of the division of 13 by 5. I used the subtraction to show taking out the "parts" of 13 that can be removed in increments of 5.

-Lee
 
ok, gotcha yeah I could've done 10 % 3, it's 1 (and all others) no problem. But the 3 % 4 threw me off a bit. I know there is no remainder because 4 can't go into 3, but I was wrong in assuming this solved as 0 too. I was looking all over on Google and reading about REM, but I didn't see any examples with the left operand smaller than the right. Now I know that it will evaluate as the left operand in cases like these.

Thanks again!
Scott
 
I know there is no remainder because 4 can't go into 3

That's what makes 3 the remainder.

lee1210s "3-0*4" basically says, since I can only take four away from three zero time, I'm still left with three.

EDIT: You can think of it as a pseudo code loop.

Code:
Is the left operand greater than the right operand?
If yes, subtract the right operand from the left and repeat.
Else return the left operand

B
 
I know there is no remainder because 4 can't go into 3, but I was wrong in assuming this solved as 0 too.

There is a remainder! It's 3! The result of the division is 0. That is not the remainder. The remainder is what's "left over" after "whole division".

-Lee
 
There is a remainder! It's 3! The result of the division is 0. That is not the remainder. The remainder is what's "left over" after "whole division".

-Lee

Ahhh, okay, now I see where you're coming from with this remainder business. Because 4 can't be taken away from 3, then 3 is left over, it "remains", thus 3 is the "remainder". Cool. Thanks, never thought of it that way, just thought of the division.:D
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.