Thread: Help with evaluating expressions and showing their hierarchy

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    25

    Help with evaluating expressions and showing their hierarchy

    As the name suggests, I'm new and working my way through a C book for which I don't have an answer key, so I was wondering if any of you could look through my work and tell me if it's correct:

    Evaluate the following expressions and show their hierarchy:

    1.
    Code:
    g=big/2+big*4/big-big+abc/3
    (abc=2.5, big=2, assume g to be a float)

    MY ANSWER:
    g=2/2+2*4/2-2+2.5/3g=1+2*4/2-2+2.5/3 OPERATION: /
    g=1+8/2-2+2.5/3 OPERATION: *
    g=1+4-2+2.5/3 OPERATION: /
    g=1+4-2+? 2.5/3=8.3333333... HOW DO FLOATS DEAL WITH THIS? THAT'S AS FAR AS I COULD GET WITH THIS ONE.



    2.
    Code:
    on=ink*act/2+3/2*act+2+tig
    (ink=4, act=1, tig=3.2, assume on to be an int)

    MY ANSWER:
    on=4*1/2+3/2*1+2+3.2on=4/2+3/2*1+2+3.2 OPERATION: *
    on=2+3/2*1+2+3.2 OPERATION: /
    on=2+1*1+2+3.2 OPERATION: /
    on=2+1+2+3.2 OPERATION: *
    on=3+2+3.2 OPERATION: +
    on=5+3.2 OPERATION: +
    on=5.0+3.2 OPERATION: +
    on=8



    3.
    Code:
    s=qui*add/4-6/2+2/3+6/god
    (qui=4, add=2, god=2, s is an int)

    MY ANSWER:
    s=4*2/4-6/2+2/3+6/2s=8/4-6/2+2/3+6/2 OPERATION: *
    s=2-6/2+2/3+6/2 OPERATION: /
    s=2-3+2/3+6/2 OPERATION: /
    s=2-3+0+6/2 OPERATION: /
    s=2-3+0+3 OPERATION: /
    s=-1+0+3 OPERATION: -
    s=-1+3 OPERATION: +
    s=2



    4.
    Code:
    s=1/3*a/4-6/2+2/3*6/g
    (a=4, g=3, s is an int)

    MY ANSWER:s=1/3*4/4-6/2+2/3*6/3s=0*4/4-6/2+2/3*6/3 OPERATION: /
    s=0/4-6/2+2/3*6/3 OPERATION: *
    s=0-6/2+2/3*6/3 OPERATION: /
    s=0-3+2/3*6/3 OPERATION: /
    s=0-3+0*6/3 OPERATION: *
    s=0-3+0*6/3 OPERATION: *
    s=0-3+0/3 OPERATION: /
    s=0-3+0 OPERATION: -
    s=-3+0 OPERATION: +
    s=-3






    Thanks for your help, everyone.
    Last edited by newbcore; 12-04-2008 at 12:09 AM. Reason: Oh my god, that took me well over an hour to type, and it looks like soup. Give me a few minutes to edit.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    You have this (2.5 = 0.83333...)
    g=1+4-2+2.5/3
    g=3+0.8333... = 3.833....
    Which is the answer. It actually does this:
    g=3.0 + 0.8333... = 3.8333...
    turning the int to float. So if you have "float operator" int the int becomes float and the operation is done normally

    (The actual value of 0.8333... will be something really CLOSE to that. Read about floating point values for more information. Or just make a really small/easy program and see by yourself)

    As you do here! Don't know why the above confuses you...
    on=5.0+3.2 OPERATION: +
    on=8.2
    on=8

    What operation is done first isn't known. Doesn't really matter (except for overflows, which does). The sure thing is that *, / is done first and afterwards +, -. As you do in math.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    25
    Thank you very much.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    25
    Ok, so here's another question I have. This is the expression I'm working on.

    a=b=c=3+4

    Am I right about these statements?
    1. For the first =, the left operand is a, and the right is b or b=c=3+4. The left operand is unambiguous, and the right is not.
    2. For the second =, the left operand is b or a=b, and the right is c or c=3+4. The left operand and right operand are both ambiguous.
    3. For the third =, the left operand is c or a=b=c.

    So, is the right operand for the third = 3, or is it 3+4? Or could it be either?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Assignment always works right to left, so 7 will get assigned to each variable (the value of =, as an operator, is the value that is assigned). So for the first =, a is the left operand, and the right is (b=c=3+4), and so on.

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    25
    Thanks for the help tabstop.

Popular pages Recent additions subscribe to a feed