Posts: 57
Threads: 13
Joined: Aug 2005
Is there any simple mathematical operator in QBasic (or FreeBasic) that will instantly compare and return the larger of two numbers?
So that if:
a = 5
b = 9
Print LargerNumber (a,b)
Would just automatically print 9?
I know one could easily write a very small function for this purpose, but I want to know if there is an inherent function in the QB or FB syntax that accomplishes it automatically.
Thanks!
Posts: 650
Threads: 26
Joined: Aug 2004
Code:
IF a > b THEN c = a ELSE c = b
quote="whitetiger0990"]whitetiger is.. WHITE POWER!!! [/quote]
Here
Posts: 961
Threads: 81
Joined: Apr 2005
You know, to me both those code snippets were useless.
Code:
Declare Sub LargerNumber(x,y)
Dim shared x
Dim shared y
Dim shared Result
x=5
y=10
LargerNumber (x,y)
Print Result
sleep
end
'.....
Sub LargerNumber(x,y)
If x>y then
Result=x
elseif y>x then
Result=y
elseif x=y then
Print "Both are the same"
endif
End Sub
Just to be sure I have tested this in FreeBasic. I originally had a function and a call as follows:
Code:
Result=LargerNumber(x,y)
Print Result
...but that gave me "0" as the output. Changing the function to a sub and changing the way I called it:
Code:
LargerNumber(x,y)
Print Result
...and it worked this way. Of course, feel free to change things as you see fit.
Enjoy.
>anarky
Screwing with your reality since 1998.
Posts: 484
Threads: 14
Joined: Apr 2005
iif
Code:
Dim As Integer a, b, c
a = 5
b = 10
c = iif(a > b, a, b)
Print c
Sleep
EDIT: whoops, i see chaos already did it.
EVEN MEN OF STEEL RUST.
Posts: 544
Threads: 27
Joined: Jan 2005
chaOs', Dio's and Yetifoot's solutions give a wrong answer for the two values equal to each other.
Anarky's solution is the only complete one.
Here is my proposed QuickBASIC 4.5 solution:
Code:
CLS
Greater$ = " Neither, both are equal"
INPUT "Enter two numers, separated by a comma"; A , B
IF A>B THEN Greater$ = STR$(A) ELSE IF A<B THEN Greater$ = STR$(B)
PRINT " The greater of"; A;"and";B;"is: Greater$
Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Posts: 484
Threads: 14
Joined: Apr 2005
I don't understand. He didn't specify he needed to know if they're equal.
EVEN MEN OF STEEL RUST.
Posts: 544
Threads: 27
Joined: Jan 2005
No, he didn't, but he asked to know which was greater. The result when they are equal, with your code, is to report that one is greater, even when they are both equal. So, by implication, the question, 'Which of these two numbers is greater?", must be answered with "Neither, they are both equal", or some such answer, when that case exisits.
Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Posts: 961
Threads: 81
Joined: Apr 2005
Now who's wrong?
Screwing with your reality since 1998.