Joined: 11 Jan 2006 Posts: 83 Location: Lower Saxony (DE)
While researching for my other problem I stumbled across the MIN-, MAX-, MEAN-, SUM- and other intrinsic functions. So there's acute need, just because I'm inquisitive:
Some manuals tell, that the MEAN or the SUM function can be used with arrays appending the (ALL) option.
But no manual does so when explaining the MIN-, MAX- and other functions which might be useful here as well.
According to my COBOL-Book functions supporting the (ALL) option are MEAN, MEDIAN, MIDRANGE, STANDARD-DEVIATION, VARIANCE, SUM
Functions that don't are MAX, MIN, ORD-MAX, ORD-MIN, RANGE.
Are my book and other online documentations incomplete or does IBM Enterprise COBOL for z/OS 4.2 not support these options (yet)?
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
The 4.2 manual has a detailed explanation of ALL in relation to Intrinsic Functions. If you still have problems after digesting and experimenting, show us what you tried, what happened, and what you thought would happen, please.
Joined: 14 Jan 2008 Posts: 2501 Location: Atlanta, Georgia, USA
SUM is really handy when programmatically calculating the OCCURS in an ARRAY (numerics only) which is not associated with a group. EG:
Code:
03 WS-NUMERIC-TBL OCCURS 100 TIMES
INDEXED BY X-WS-NT, X-WS-NT-MAX
PIC 9(07).
03 WS-FWORD PIC 9(08) BINARY.
INITIALIZE WS-NUMERIC.
INITIALIZE WS-NUMERIC-TBL REPLACING NUMERIC DATA BY 1.
COMPUTE WS-FWORD = SUM(WS-NUMERIC-TBL (ALL)).
SET X-WS-NT-MAX TO WS-FWORD.
The value in X-WS-NT-MAX is 100, matching the defined OCCURS.
Updated 2013/11/15 -
Apologies, but the example above will NOT work. Alternatively, the following will, providing that your COBOL version is a minimum of COBOL for OS/390 & VM V2R2 (introduced about 13 years ago), which supports ADDRESSes in WORKING-STORAGE.
Code:
03 WS-NUMERIC-TBL OCCURS 100 TIMES
INDEXED BY X-WS-NT, X-WS-NT-MAX
PIC 9(07).
03 WS-NUMERIC-TBL-END PIC X(01).
03 WS-FWORD PIC 9(08) BINARY.
03 WS-START-POINTER POINTER.
03 WS-START-FWORD REDEFINES WS-START-POINTER
PIC 9(08) BINARY.
03 WS-END-POINTER POINTER.
03 WS-END-FWORD REDEFINES WS-END-POINTER
PIC 9(08) BINARY.
*
SET WS-START-POINTER TO ADDRESS OF WS-NUMERIC-TBL (1).
SET WS-END-POINTER TO ADDRESS OF WS-NUMERIC-TBL-END.
SUBTRACT WS-START-FWORD FROM WS-END-FWORD
GIVING WS-FWORD.
DIVIDE WS-FWORD BY LENGTH OF WS-NUMERIC-TBL (1)
GIVING WS-FWORD.
SET X-WS-NT-MAX TO WS-FWORD.
The value in X-WS-NT-MAX is 100, matching the defined OCCURS.