4 *Y+Z ™+

!Ü 8 *X -5 *Z

™Q2

`

Select the VAR menu and start the program.

J%EXCO%

Minimum and Maximum Array Elements

This section contains two programs that find the minimum or maximum element of an array:

MNX uses a DO…UNTIL…END (indefinite) loop.

MNX2 uses a FOR…NEXT (definite) loop.

MNX (Minimum or Maximum Element—Version 1)

MNX finds the minimum or maximum element of an array on the stack.

Level 1

Level2

Level 1

 

 

 

 

[[ array ]]

[[ array ]]

Zmin or Zmax

 

 

 

 

Techniques used in MNX

DO…UNTIL…END (indefinite loop). The DO clause contains the sort instructions. The UNTIL clause contains the system-flag test that determines whether to repeat the sort instructions.

User and system flags for logic control:

User flag 10 defines the sort: When flag 10 is set, MNX finds the maximum element; when flag 10 is clear, it finds the minimum element. You determine the state of flag 10 at the beginning of the program.

System flag 64, the Index Wrap Indicator flag, determines when to end the sort. While flag 64 is clear, the sort loop continues. When the index invoked by GETI wraps back to the first array element, flag 64 is automatically set, and the sort loop ends.

Nested conditional. An IF…THEN…END conditional is nested in the DO…UNTIL…END conditional, and determines the following:

Whether to maintain the current minimum or maximum element, or make the current element the new minimum or maximum.

The sense of the comparison of elements (either < or >) based on the status of flag 10.

Custom menu. MNX builds a custom menu that lets you choose whether to sort for the minimum or maximum element. Key 1, labeled %MAX%, sets flag 10. Key 2, labeled %MIN%, clears flag 10.

Logical function. MNX executes XOR (exclusive OR) to test the combined state of the relative value of the two elements and the status of flag 10.

2-16 RPL Programming Examples