(11C) Finding biggest of 3 integers
10-20-2018, 10:48 AM (This post was last modified: 10-20-2018 11:12 AM by Dieter.)
Post: #2
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: (11C) Finding biggest of 3 numbers
(10-20-2018 05:41 AM)Gamo Wrote:  Program to find the biggest number of 3 integers.

Of any three numbers, not only integers.

Gamo, do you remember the following sequence?

With two numbers in X and Y, X≤Y? X<>Y checks if X is the smaller of the two. If it is, both are swapped.
So this sequence always returns the larger number in X and the smaller one in Y.

Likewise, X>Y? X<>Y returns the smaller number in X and the larger one in Y.

Now assume there are three different numbers in X, Y and Z.
X>Y? X<>Y will put the larger one in Y and the smaller one in X.

Roll down the stack one level to compare this larger number with the third one.
X≤Y? X<>Y will put the largest of all three in X.

So the program simply is:

Code:
01 LBL A 02 X>Y? 03 X<>Y 04 R↓ 05 X≤Y? 06 X<>Y 07 RTN

Or on the 12C and others without an X>Y? test:

Code:
01 X≤Y? 02 X<>Y 03 X<>Y 04 R↓ 05 X≤Y? 06 X<>Y 07 GTO 00

You can easily extend this example. This is what we get:

Return the largest of two numbers:
Code:
01 LBL A 02 X≤Y?  03 X<>Y 04 RTN

Return the largest of three numbers:
Code:
01 LBL A 02 X>Y? 03 X<>Y 04 R↓ 05 X≤Y? 06 X<>Y 07 RTN

Return the largest of four number:
Code:
01 LBL A 02 X>Y? 03 X<>Y 04 R↓ 05 X>Y? 06 X<>Y 07 R↓ 08 X≤Y? 09 X<>Y 10 RTN

Dieter
 « Next Oldest | Next Newest »

 Messages In This Thread (11C) Finding biggest of 3 integers - Gamo - 10-20-2018, 05:41 AM RE: (11C) Finding biggest of 3 numbers - Dieter - 10-20-2018 10:48 AM RE: (11C) Finding biggest of 3 integers - Gamo - 10-21-2018, 03:13 AM

User(s) browsing this thread: 1 Guest(s)