1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Help with problem in c++ class

Discussion in 'Programming & Webmastering' started by Asylum, Oct 10, 2012.

  1. Asylum

    Asylum

    Joined:
    Jul 18, 2008
    Messages:
    2,809 (1.23/day)
    Thanks Received:
    683
    Location:
    South Carolina
    Our teacher gave us some problems to work for class but I dont see anywhere in the book that explains these.
    Im sure it is something simple that im overlooking because im good in math.
    Could someone show me how to work these.
    Thanks in advance.


    Following the pseudocode below, what is the value of X if Y = 10?

    X=0
    If Y > 10 then
    X=5
    Else
    If Y < 20 then
    X=7
    Else
    X=9
     
  2. Asylum

    Asylum

    Joined:
    Jul 18, 2008
    Messages:
    2,809 (1.23/day)
    Thanks Received:
    683
    Location:
    South Carolina
    Ok I think I see it already.

    X= 7 correct?
     
  3. Vinska

    Vinska

    Joined:
    Jul 23, 2011
    Messages:
    1,419 (1.19/day)
    Thanks Received:
    1,259
    Location:
    Kaunas, Lithuania
    Yes, You are correct. Also, the last branch will never trigger no matter what is the value of Y.

    And to finding an answer: if You can't figure out the end result by reading a description/pseudocode/whatever, but can see/understand the algorithm, just write a small test program and simply get the result :p
     
    Asylum says thanks.
    Crunching for Team TPU
  4. MrSeanKon

    MrSeanKon New Member

    Joined:
    Nov 14, 2006
    Messages:
    267 (0.09/day)
    Thanks Received:
    69
    Location:
    Athens in love with Anna :)
    X=5 for all Y values greater than 10.
    For values less than 10 e.g. Y=8, Y=9, Y=9.999999999 (not exactly Y=10) X is always 9.
    10 value for Y is a "critical" value where the second if/else conditional works.
     
  5. Aleksander

    Joined:
    Dec 2, 2009
    Messages:
    3,254 (1.82/day)
    Thanks Received:
    304
    I don't even get it why you asked?
     
  6. 3870x2

    3870x2

    Joined:
    Feb 26, 2008
    Messages:
    4,875 (2.00/day)
    Thanks Received:
    689
    Location:
    Joplin, Mo
    He didn't understand it at first. I thought it was pretty easy. I am sure there was something very specific he wasn't understanding.
     
  7. W1zzard

    W1zzard Administrator Staff Member

    Joined:
    May 14, 2004
    Messages:
    14,969 (3.92/day)
    Thanks Received:
    11,787
    much more readable once you indent it

    Code:
    X=0
    If Y > 10 then
      X=5
    Else
      If Y < 20 then 
        X=7
      Else
        X=9 
    
    when using a language that uses closing blocks for if it will be even more readable
    Code:
    X=0
    If Y > 10 then
      X=5
    Else
      If Y < 20 then 
        X=7
      Else
        X=9 
      Endif
    Endif
    
     
  8. Drone

    Drone

    Joined:
    Sep 1, 2010
    Messages:
    2,743 (1.81/day)
    Thanks Received:
    1,468
    It makes no sense at all, especially the first else.

    Else means that Y<10 and "if Y<20" is redundant. Because if Y<10 then of course Y<20, that part is captain obvious.
     
  9. Vinska

    Vinska

    Joined:
    Jul 23, 2011
    Messages:
    1,419 (1.19/day)
    Thanks Received:
    1,259
    Location:
    Kaunas, Lithuania
    @Drone

    Uhm, it's not Y<10 and Y<20
    it's Y>10 and Y<20 [​IMG]

    Yet, the last else is redundant, 'cause it will never trigger (as I said in my 1st reply ;) )
     
    Crunching for Team TPU
  10. Drone

    Drone

    Joined:
    Sep 1, 2010
    Messages:
    2,743 (1.81/day)
    Thanks Received:
    1,468
    No, it's y<10. They wrote:

    It means that whatever comes after the "else" operator can happen only when y<10
     
  11. Vinska

    Vinska

    Joined:
    Jul 23, 2011
    Messages:
    1,419 (1.19/day)
    Thanks Received:
    1,259
    Location:
    Kaunas, Lithuania
    @Drone

    > No, it's y<10. They wrote:
    Uhm, squeeze me, but aren't You contradicting Yourself?
     
    Crunching for Team TPU
  12. Drone

    Drone

    Joined:
    Sep 1, 2010
    Messages:
    2,743 (1.81/day)
    Thanks Received:
    1,468
    No need to play word games here. Else means that y is less or equal 10. Period.
     
  13. Vinska

    Vinska

    Joined:
    Jul 23, 2011
    Messages:
    1,419 (1.19/day)
    Thanks Received:
    1,259
    Location:
    Kaunas, Lithuania
    Okay, I won't be playing word games.

    Graphical representation:
    [​IMG]

    Edit: OK, now I see Your point. That thing is quite obvious when looking at that "code".
    Yet, you wrote it very ambiguously. Ambiguously enough to confuse me into thinking You were trying to say a completely different thing. If You would be less ambiguous next time, I would be grateful. Thank You! :)
     
    Last edited: Oct 16, 2012
    Crunching for Team TPU
  14. 3870x2

    3870x2

    Joined:
    Feb 26, 2008
    Messages:
    4,875 (2.00/day)
    Thanks Received:
    689
    Location:
    Joplin, Mo
    Would look a lot easier if:

    X = 0;
    if (Y > 10) then x = 5;
    elseif ( Y < 20 ) then X = 7;
    else x = 9;
    end if

    Sometimes pseudocode is just more confusing.
     

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page