Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple operator for returning larger of two numbers?
#1
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!
Reply
#2
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
Reply
#3
Code:
IF a > b THEN c = a ELSE c = b
quote="whitetiger0990"]whitetiger is.. WHITE POWER!!! [/quote]
Here
Reply
#4
HAHA
Reply
#5
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
Screwing with your reality since 1998.
Reply
#6
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.
[Image: chav.gif]
Reply
#7
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.
Reply
#8
I don't understand. He didn't specify he needed to know if they're equal.
EVEN MEN OF STEEL RUST.
[Image: chav.gif]
Reply
#9
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.
Reply
#10
Now who's wrong?
Screwing with your reality since 1998.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)