Trouble finding correct value

This is a discussion on Trouble finding correct value within the C++ Programming forums, part of the General Programming Boards category; Hey all, I know this is a newb question here but I'm having trouble finding the answer to this equation ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    22

    Trouble finding correct value

    Hey all, I know this is a newb question here but I'm having trouble finding the answer to this equation posed in one of my past year exam scripts.

    Code:
    int z, x = 5, y = -10, a = 4, b = 2; 
    //x = 5
    //y = -10
    //a = 4
    //b = 2
    
    z = x++ - --y * b / a;
    When I solve this equation I get an answer of 0 (zero) but when I put it in the compiler, the value returned is 10.

    Heres how I solved it:

    z = x++ - (--y) * b / a;
    z = 5++ - (--(-10)) * 2 / 4;
    z = 6 - (-11) * 2 / 4;
    z = 6 - (-11) * 0 // at this point 2/4 yields 0 correct?
    z = 6 + 11 * 0
    z = 0;
    but I am sadly mistaken as the answer is 10 according to the compiler.
    Can any one help me out here?
    Please and thank you. Any feedback is greatly appreciated.

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    x++
    This increments x after the expression is evaluated.
    --y * b / a;
    Y is decremented first, then multiplication and division are done in one go, left to right.

    z = 6 + 11 * 0
    z = 0;
    ORDER OF OPERATIONS!!!

    Ten is what I got, btw.
    Consider this post signed

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    22
    Quote Originally Posted by bernt View Post
    This increments x after the expression is evaluated.

    Y is decremented first, then multiplication and division are done in one go, left to right.


    ORDER OF OPERATIONS!!!

    Ten is what I got, btw.
    Thank you thank you, I finally got 10.

    x++ - --y * b / a;
    5 - (-11) * 2 / 4;
    5 - (-22)/4
    5 - (-5)
    = 5 + 5 = 10.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Code:
    z = 5++ - (--(-10)) * 2 / 4;
    That is not a legal C++ statement.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    22
    Quote Originally Posted by Memloop View Post
    Code:
    z = 5++ - (--(-10)) * 2 / 4;
    That is not a legal C++ statement.
    I just wrote that to denote the brackets, to help visualise that (-10) would have been decremented.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seemingly correct printf statements bringing up errors
    By hpteenagewizkid in forum C Programming
    Replies: 9
    Last Post: 11-08-2006, 11:13 AM
  2. Trouble using ArrayList
    By stevespai in forum C# Programming
    Replies: 12
    Last Post: 07-31-2006, 11:17 AM
  3. Trouble with DMA Segmentation Faults
    By firestorm717 in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 09:20 PM
  4. How can I verify if a date is correct?
    By ^DJ_Link^ in forum C Programming
    Replies: 11
    Last Post: 01-13-2004, 11:47 AM
  5. Replies: 1
    Last Post: 12-01-2001, 07:17 AM

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