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

Joined:
Jul 18, 2008
Messages:
2,862 (1.10/day)
705
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.

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

Joined:
Jul 18, 2008
Messages:
2,862 (1.10/day)
705
Location:
South Carolina
Ok I think I see it already.

X= 7 correct?

3. ### Vinska

Joined:
Jul 23, 2011
Messages:
1,515 (1.01/day)
1,428
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

Asylum says thanks.
4. ### MrSeanKonNew Member

Joined:
Nov 14, 2006
Messages:
267 (0.08/day)
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,261 (1.55/day)
310
I don't even get it why you asked?

6. ### 3870x2

Joined:
Feb 26, 2008
Messages:
4,876 (1.78/day)
690
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. ### W1zzardAdministratorStaff Member

Joined:
May 14, 2004
Messages:
15,609 (3.78/day)
13,228
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

Joined:
Sep 1, 2010
Messages:
3,125 (1.71/day)
2,071
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

Joined:
Jul 23, 2011
Messages:
1,515 (1.01/day)
1,428
Location:
Kaunas, Lithuania
@Drone

Uhm, it's not Y<10 and Y<20
it's Y>10 and Y<20

Yet, the last else is redundant, 'cause it will never trigger (as I said in my 1st reply )

10. ### Drone

Joined:
Sep 1, 2010
Messages:
3,125 (1.71/day)
2,071
No, it's y<10. They wrote:

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

11. ### Vinska

Joined:
Jul 23, 2011
Messages:
1,515 (1.01/day)
1,428
Location:
Kaunas, Lithuania
@Drone

> No, it's y<10. They wrote:
Uhm, squeeze me, but aren't You contradicting Yourself?

12. ### Drone

Joined:
Sep 1, 2010
Messages:
3,125 (1.71/day)
2,071
No need to play word games here. Else means that y is less or equal 10. Period.

13. ### Vinska

Joined:
Jul 23, 2011
Messages:
1,515 (1.01/day)
1,428
Location:
Kaunas, Lithuania
Okay, I won't be playing word games.

Graphical representation:

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
14. ### 3870x2

Joined:
Feb 26, 2008
Messages:
4,876 (1.78/day)
690
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.