I am running two errors on compile that I cannot figure out. "calcavg.cob: in paragraph 'CALC-AVG':" and "calcavg.cob:30: error: 'INTS' requires one subscript" I cannot figure these out. below is the code for the program
IDENTIFICATION DIVISION.
PROGRAM-ID. ASSIGNMENT4.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUM-OF-INTS PIC 99.
01 INT-ARRAY.
05 INTS OCCURS 15 TIMES PIC 999.
01 TOTAL PIC 9(5)V99.
01 AVG PIC 9(3)V99.
01 COUNTER PIC 99.
PROCEDURE DIVISION.
MAIN-LOGIC.
DISPLAY "Enter number of integers to average (2-15): ".
ACCEPT NUM-OF-INTS.
PERFORM ACCEPT-INTS
PERFORM CALC-AVG
DISPLAY "Average: " AVG DISPLAY "ZZZ.99".
STOP RUN.
ACCEPT-INTS.
DISPLAY "Enter integers to average: ".
PERFORM VARYING COUNTER FROM 1 BY 1 UNTIL COUNTER>NUM-OF-INTS
DISPLAY "Integer " COUNTER ": "
ACCEPT INTS(COUNTER)
END-PERFORM.
CALC-AVG.
COMPUTE TOTAL = FUNCTION SUM(INTS(1:NUM-OF-INTS))
COMPUTE AVG = TOTAL / NUM-OF-INTS.
EXIT.
COMPUTE TOTAL = FUNCTION SUM(INTS(1:NUM-OF-INTS))
(1:NUM-OF-INTS)
is not a subscript. This format is used for reference modification; that is, to select a substring from a data-item that is USAGE DISPLAY
. You appear to be using it as an array slice for the entire table.
Use this instead:
05 INTS OCCURS 2 to 15 TIMES
DEPENDING ON NUM-OF-INTS PIC 999.
COMPUTE TOTAL = FUNCTION SUM(INTS(ALL))
The DEPENDING ON
limits the number of items to be added together with the ALL
subscript. The ALL
subscript with SUM
will add that many numbers together. The resulting value then be moved to TOTAL
.
To get around the problem of not recognizing ALL
. Use the following, to replace the COMPUTE
statement:
05 INTS OCCURS 2 to 15 TIMES
DEPENDING ON NUM-OF-INTS INDEXED BY INTS-IDX PIC 999.
MOVE 0 TO TOTAL
PERFORM VARYING INTS-IDX FROM 1 BY 1
UNTIL INTS-IDX > NUM-OF-INTS
ADD INTS(INTS-IDX) TO TOTAL
END-PERFORM