COBOL Manual for TNS and TNS/R Programs
Intrinsic Functions
HP COBOL Manual for TNS and TNS/R Programs—522555-006
14-33
MEDIAN Function
The returned value is the value that would be in the middle of the list if the arguments
were sorted numerically (according to the rules for comparing simple conditions). If
there are an odd number of arguments, at least half of them are less than or equal to
the returned value, and at least half of them are greater than or equal to the returned
value. If there are an even number of arguments, the returned value is the arithmetic
mean of the two middle arguments. (For the definition of arithmetic mean, see MEAN
Function).
Example 14-23. MEDIAN Function
Code:
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUMERIC-ARRAY.
05 PICTURE 9(1) VALUE 4.
05 PICTURE 9(1) VALUE 9.
05 PICTURE 9(1) VALUE 3.
05 PICTURE 9(1) VALUE 7.
05 PICTURE 9(1) VALUE 5
01 NUM-ARRAY REDEFINES NUMERIC-ARRAY.
05 NUM OCCURS 5 TIMES PICTURE 9(1).
01 A PICTURE S9V9.
PROCEDURE DIVISION.
MOVE FUNCTION MEDIAN (NUM(ALL)) TO A.
DISPLAY A.
MOVE FUNCTION MEDIAN (NUM(1) NUM(3) NUM(5)) TO A.
DISPLAY A.
MOVE FUNCTION MEDIAN (4 1 7 5 2 3 6) TO A.
DISPLAY A.
MOVE FUNCTION MEDIAN (4 1 6 5 2 3) TO A.
DISPLAY A.
Output:
5.0
4.0
4.0
3.5