在COBOL linux中计算平均值-计算语句出错

6mzjoqzu  于 2023-03-29  发布在  Linux
关注(0)|答案(1)|浏览(127)

我在编译时运行了两个错误,我无法弄清楚。“在段落‘CALC-AVG’:”和“calcavg.cob:30:错误:'INTS'需要一个下标”我无法计算这些.下面是程序的代码

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.
nue99wik

nue99wik1#

COMPUTE TOTAL = FUNCTION SUM(INTS(1:NUM-OF-INTS))

(1:NUM-OF-INTS)不是下标,此格式用于引用修改;也就是说,从USAGE DISPLAY数据项中选择一个子字符串,您似乎将它用作整个表的数组切片。
请使用以下命令:

05  INTS OCCURS 2 to 15 TIMES
        DEPENDING ON NUM-OF-INTS PIC 999.

COMPUTE TOTAL = FUNCTION SUM(INTS(ALL))

DEPENDING ON限制了要与ALL下标相加的项目数。ALL下标与SUM将把这么多数字相加在一起。然后将结果值移动到TOTAL
要解决无法识别ALL的问题,请使用以下语句替换COMPUTE语句:

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

相关问题