Top
NetCOBOL V11.0 Syntax Samples
FUJITSU Software

1.49 MAX and MIN Function

The MAX function returns the maximum value of an argument consisting of several different values. Likewise, the MIN function returns the minimum value from the list of values that is the argument to the function. A table of values may be passed as an argument to these functions, as illustrated below.

000010 @OPTIONS MAIN
000020*----------------------------------------------------------------------
000030* In this sample, the MAX and MIN functions are used to obtain the 
000040* maximum and minimum values of three input values.
000050*----------------------------------------------------------------------
000060 IDENTIFICATION   DIVISION.
000070 PROGRAM-ID.      SAMPLE.
000080 DATA             DIVISION.
000090 WORKING-STORAGE  SECTION.
000100 01 .
000110    02 VAL        OCCURS 3 TIMES PIC 9(4).
000120 01 MAX-VAL       PIC 9(5).
000130 01 MIN-VAL       PIC 9(5).
000140 01 COUNTER       PIC 9(1).
000150 PROCEDURE        DIVISION.
000160     DISPLAY "Please input three values.".
000170     PERFORM TEST BEFORE
000180             VARYING COUNTER FROM 1 BY 1 UNTIL COUNTER > 3
000190       DISPLAY "Value (up to four digits) >> " WITH NO ADVANCING
000200       ACCEPT VAL(COUNTER)
000210     END-PERFORM.
000220     DISPLAY " ".
000230     DISPLAY "The maximum and minimum values of the input data are 
000235-            "determined."
000240*----------------------------------------------------------------------
000250* The maximum and minimum values are determined.
000260*----------------------------------------------------------------------
000270     COMPUTE MAX-VAL = FUNCTION MAX (VAL(ALL)).
000280     COMPUTE MIN-VAL = FUNCTION MIN (VAL(ALL)).
000290*----------------------------------------------------------------------
000300     DISPLAY "The maximum value is " MAX-VAL ".".
000310     DISPLAY "The minimum value is " MIN-VAL ".".
000320 END PROGRAM SAMPLE.