Why is output 0

Discussion in 'Mac Programming' started by cybrscot, Jan 18, 2011.

  1. cybrscot macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #1
    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 ;
    	
    }	
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    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
     
  3. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #3
    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?
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    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
     
  5. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #5
    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
     
  6. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #6
    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
     
  7. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #7
    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
     
  8. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #8
    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
     

Share This Page