Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: C programming question

  1. Jul 5, 2011 #1
    1. The problem statement, all variables and given/known data

    Evaluate the following expressions

    (i) 1 - 2/3 + 4 - 5

    (ii) 1 - 2/3 + 4.0 - 5

    (iii) 1 - 2/3.0 + 4 - 5

    (iv) 1 + 4*3/2

    (v) 1 + 3/2*4

    (vi) 'h' - 'e' + 'l' - 'p'

    3. The attempt at a solution

    The answers for each question respectively are
    0
    0.0
    -.666667
    7
    5
    -1

    I am completely lost here. I have no idea what the differences between the first 3 are or the 4th and 5th. There is nothing about this in my notes.

    Any advice is greatly appreciated.
     
  2. jcsd
  3. Jul 5, 2011 #2

    gb7nash

    User Avatar
    Homework Helper

    i) All of the numbers in the expression are integers. Since this is so, we confine the result of each calculation to be an integer. 2/3 = 0 by chopping off the decimal.

    ii) Since a 4.0 is introduced, you are now working with a float that has two significant figures. After combining the terms, you'll end up with 0.0 instead of 0.

    iii) This one I'm not quite sure on. You're performing a division on a float, so after dividing out, you'll be left with a float. I'm not quite sure how many decimal places you keep though.

    iv,v) PEMDAS. Perform multiplication/division from left to right. Then addition. Since we're working with all integers again, you should see the answers.

    vi) Convert each character to its appropriate ASCII code.
     
    Last edited: Jul 5, 2011
  4. Jul 7, 2011 #3
    Thanks, I'm still not quite sure on (v) and (iii)
     
  5. Jul 7, 2011 #4
    On (v), although I agree the order of operations is PEMDAS, I would think the division in this case is happening before the multiplication because 3/2 = 1 (integer division), so it would yield 1+1*4 = 5.
     
  6. Jul 7, 2011 #5

    I like Serena

    User Avatar
    Homework Helper

    Hi Maybe_Memorie! :smile:

    Have you moved on to C programming?


    First C evaluates multiplications and divisions from left to right.
    In this case there is only 1 division.

    If the arguments of a division are both integers, the result is a truncated integer division.
    If either argument (or both) have a point in them (that is, they are "floating point"), the result is floating point.
    In this case 2/3.0 = 0.666666666666667
    (Usually you will always have 15 significant digits, so gb7nash's remark is off.)

    Then C evaluates all additions and subtractions from left to right.

    So:
    1 - 2/3.0 + 4 - 5
    = 1 - 0.666666666666667 + 4 - 5
    = 0.333333333333333 + 4 - 5
    = 4.33333333333333 - 5
    = -0.66666666666667


    In this case we're looking at integer division, and we evaluate mul together with div left to right.
    1 + 3/2*4
    = 1 + 1*4
    = 1 + 4

    And then additions and subtractions left to right:
    1 + 4 = 5
     
    Last edited: Jul 7, 2011
  7. Jul 7, 2011 #6

    gb7nash

    User Avatar
    Homework Helper

    It does. In PEMDAS, you do multiplication/division left to right. The division sign occurs before the multiplication sign like you said, so you would end up getting 1+1*4 = 5

    I learned something new today.
     
  8. Jul 9, 2011 #7

    I like Serena

    User Avatar
    Homework Helper

    Hey Maybe_Memorie! :smile:

    Are you satisfied with the answers?
    (I always like to feel a thread was finished satisfactorily before moving on to new problems. :wink:)
     
  9. Jul 18, 2011 #8
    Thank you very much! :)

    (Sorry for taking so long to reply, I don't always have internet access :redface: )
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook