Thread: Help with evaluating expressions and showing their hierarchy

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

Popular pages Recent additions subscribe to a feed