Qbasicnews.com
Simple operator for returning larger of two numbers? - Printable Version

+- Qbasicnews.com (http://qbasicnews.com/newforum)
+-- Forum: QBasic (http://qbasicnews.com/newforum/forum-4.html)
+--- Forum: QB Discussion & Programming Help (http://qbasicnews.com/newforum/forum-11.html)
+--- Thread: Simple operator for returning larger of two numbers? (/thread-8916.html)

Pages: 1 2 3


Simple operator for returning larger of two numbers? - paleozord - 02-21-2006

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!


Simple operator for returning larger of two numbers? - Anonymous - 02-21-2006

nope, nothing intrinsic.

in fb:

Code:
#Define larger_number(x,y) ( IIf( x < y, y, x ) )


? larger_number( 12, 13 )
? larger_number( 4, 2 )
? larger_number( 1, 1 )
? larger_number( 0, 234213 )
? larger_number( 2344312, 6523313 )


Sleep



Simple operator for returning larger of two numbers? - Dio - 02-21-2006

Code:
IF a > b THEN c = a ELSE c = b



Simple operator for returning larger of two numbers? - Anonymous - 02-21-2006

HAHA


Simple operator for returning larger of two numbers? - anarky - 03-05-2006

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. Big Grin

Enjoy.

>anarky


Simple operator for returning larger of two numbers? - yetifoot - 03-05-2006

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.


Simple operator for returning larger of two numbers? - Ralph - 03-06-2006

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$



Simple operator for returning larger of two numbers? - yetifoot - 03-06-2006

I don't understand. He didn't specify he needed to know if they're equal.


Simple operator for returning larger of two numbers? - Ralph - 03-06-2006

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.


Simple operator for returning larger of two numbers? - anarky - 03-06-2006

Now who's wrong?