Trying to add gm and hm calculations to cobol program

253 Views Asked by At

i'm trying to update the program by adding the following features: • Two measures of central tendency - geometric mean (GM), and harmonic mean (HM) • One measure of dispersion - root mean square (RMS)

GM = [x(1)⋅x(2)⋅x(3)⋅…⋅x(n)]^(1/n)
HM = n / SR
SR = 1/x(1) + 1/x(2) + … + 1/x(n)
RMS = sqrt(Sx2/n]
Sx2 = x(1)⋅x(1) + x(2)⋅x(2) + … + x(n)⋅x(n)

The issue is it says sqrt is not defined. I guess my compiler just doesnt accept it. Im using cobc (GnuCOBOL) 3.1.2.0 on linux.

this is my code

IDENTIFICATION DIVISION.
PROGRAM-ID. FileProcessingProgram.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT INPUT-FILE ASSIGN TO 'nums.txt'.
    SELECT OUTPUT-FILE ASSIGN TO 'output.txt'.

DATA DIVISION.
FILE SECTION.
FD  INPUT-FILE.
01  INPUT-VALUE-RECORD.
    02 IN-X PIC S9(6)V9(2).
    02 FILLER PIC X(72).

FD  OUTPUT-FILE.
01  OUTPUT-LINE PIC X(80).

WORKING-STORAGE SECTION.
77 SUM-OF-X-SQR   PIC 9(14)V9(2).
77 SUM-OF-X       PIC S9(10)V9(2).
77 N              PIC S9(4).
77 MEAN           PIC S9(6)V9(2).
77 I              PIC S9(4).
01 ARRAY-AREA.
   02 X PIC S9(6)V9(2) OCCURS 1000 TIMES.
01 OUTPUT-TITLE-LINE.
   02 FILLER PIC X(28) VALUE " MEAN AND STANDARD DEVIATION".
01 OUTPUT-UNDERLINE.
   02 FILLER PIC X(28) VALUE "----------------------------".
01 OUTPUT-COL-HEADS.
   02 FILLER PIC X(10) VALUE SPACES.
   02 FILLER PIC X(11) VALUE "DATA VALUES".
01 OUTPUT-DATA-LINE.
   02 FILLER PIC X(10) VALUE SPACES.
   02 OUT-X PIC -(6)9.9(2).
01 OUTPUT-RESULTS-LINE-1.
   02 FILLER PIC X(9) VALUE " MEAN= ".
   02 OUT-MEAN PIC -(6)9.9(2).
01 OUTPUT-RESULTS-LINE-2.
   02 FILLER PIC X(9) VALUE " STD DEV=".
   02 STD-DEVIATION PIC -(6)9.9(2).
01 OUTPUT-RESULTS-LINE-3.
   02 FILLER PIC X(9) VALUE " GEOMETRIC MEAN=".
   02 GEOMETRIC-MEAN PIC -(6)9.9(2).
01 OUTPUT-RESULTS-LINE-4.
   02 FILLER PIC X(9) VALUE " HARMONIC MEAN=".
   02 HARMONIC-MEAN PIC -(6)9.9(2).
01 OUTPUT-RESULTS-LINE-5.
   02 FILLER PIC X(9) VALUE " ROOT MEAN SQ=".
   02 ROOT-MEAN-SQUARE PIC -(6)9.9(2).

PROCEDURE DIVISION.
OPEN INPUT INPUT-FILE, OUTPUT OUTPUT-FILE.
MOVE ZERO TO IN-X.
PERFORM PROC-BODY
   UNTIL IN-X IS NOT LESS THAN 999999.99.
PERFORM END-OF-JOB.

PROC-BODY.
WRITE OUTPUT-LINE FROM OUTPUT-TITLE-LINE
   AFTER ADVANCING 0 LINES.
WRITE OUTPUT-LINE FROM OUTPUT-UNDERLINE
   AFTER ADVANCING 1 LINE.
WRITE OUTPUT-LINE FROM OUTPUT-COL-HEADS
   AFTER ADVANCING 1 LINE.
WRITE OUTPUT-LINE FROM OUTPUT-UNDERLINE
   AFTER ADVANCING 1 LINE.
MOVE ZERO TO SUM-OF-X.
READ INPUT-FILE INTO INPUT-VALUE-RECORD
   AT END PERFORM END-OF-JOB.
PERFORM INPUT-LOOP
   VARYING N FROM 1 BY 1
   UNTIL N IS GREATER THAN 1000 OR IN-X IS NOT LESS THAN 999999.98.
SUBTRACT 1 FROM N.
DIVIDE N INTO SUM-OF-X GIVING MEAN ROUNDED.
MOVE ZERO TO SUM-OF-X-SQR.
PERFORM SUM-LOOP
   VARYING I FROM 1 BY 1
   UNTIL I IS GREATER THAN N.
COMPUTE STD-DEVIATION ROUNDED = (SUM-OF-X-SQR / N) ** 0.5.
COMPUTE GEOMETRIC-MEAN ROUNDED = (X(1) * X(2) * X(3) * ... * X(N)) ** (1 / N).
COMPUTE HARMONIC-MEAN ROUNDED = N / (1 / X(1) + 1 / X(2) + ... + 1 / X(N)).
COMPUTE ROOT-MEAN-SQUARE ROUNDED = SQRT(SUM-OF-X-SQR / N).
WRITE OUTPUT-LINE FROM OUTPUT-UNDERLINE
   AFTER ADVANCING 1 LINE.
MOVE MEAN TO OUT-MEAN.
WRITE OUTPUT-LINE FROM OUTPUT-RESULTS-LINE-1
   AFTER ADVANCING 1 LINE.
WRITE OUTPUT-LINE FROM OUTPUT-RESULTS-LINE-2
   AFTER ADVANCING 1 LINE.
WRITE OUTPUT-LINE FROM OUTPUT-RESULTS-LINE-3
   AFTER ADVANCING 1 LINE.
WRITE OUTPUT-LINE FROM OUTPUT-RESULTS-LINE-4
   AFTER ADVANCING 1 LINE.
WRITE OUTPUT-LINE FROM OUTPUT-RESULTS-LINE-5
   AFTER ADVANCING 1 LINE.

INPUT-LOOP.
MOVE IN-X TO X(N), OUT-X.
WRITE OUTPUT-LINE FROM OUTPUT-DATA-LINE
   AFTER ADVANCING 1 LINE.
ADD X(N) TO SUM-OF-X.
READ INPUT-FILE INTO INPUT-VALUE-RECORD
   AT END PERFORM END-OF-JOB.

SUM-LOOP.
COMPUTE SUM-OF-X-SQR = SUM-OF-X-SQR + (X(I) * X(I)).

END-OF-JOB.
   CLOSE INPUT-FILE, OUTPUT-FILE.
   STOP RUN.

I've tried adding the features with all types of errors every attempt. Can someone please advise how to even add one or two of these features ?

1

There are 1 best solutions below

0
Rick Smith On

SQRT is an intrinsic function. Either place the word FUNCTION before SQRT or add a REPOSITORY paragraph to identify all intrinsic functions without using the FUNCTION keyword.

   ENVIRONMENT DIVISION.
   CONFIGURATION SECTION.
   REPOSITORY.
       FUNCTION ALL INTRINSIC.
   INPUT-OUTPUT SECTION.
       ...

Also, note there are intrinsic functions for some of the calculations you are doing. Please consult the documentation.


I threw together this program to suggest how the calculations may be done using an older COBOL 85 compiler. In a newer version 10 ** and LOG10 may be replaced by EXP and LOG. And, FUNCTION may be removed as mentioned earlier.

I used Wikipedia entries to determine what calculations were needed.

Notice the use of free-form input data and the use of floating-point and logarithms for some of the calculations.

Code:

   environment division.
   input-output section.
   file-control.
       select input-file assign "data.txt"
           organization line sequential
       .
       select output-file assign "report.txt"
           organization line sequential
       .
   data division.
   file section.
   fd input-file.
   01 input-record pic x(20).
   fd output-file.
   01 output-record pic x(40).
   working-storage section.
   01 n comp-5 pic 9(4) value 0.
   01 data-count comp-5 pic 9(4) value 0.
   01 data-table.
     02 data-item comp-5 pic s9(12)v9(4) value 0
       occurs 0 to 1000 times depending data-count.
   01 fp-work-table.
     02 fp-item comp-2
       occurs 0 to 1000 depending data-count.
   01 formatted-data pic zzz,zzz,zzz,zz9.9999.
   procedure division.
       perform load-table
       open output output-file
       perform print-data
       perform calculations
       close output-file
       stop run
       .
   load-table.
       open input input-file
       perform until exit
           read input-file
           at end exit perform
           not at end
               add 1 to data-count
               move function numval (input-record)
                   to data-item (data-count)
           end-read
       end-perform
       close input-file
       .
   print-data.
       perform varying n from 1 by 1
       until n > data-count
           move data-item (n) to formatted-data
           write output-record from formatted-data
       end-perform
       move space to output-record
       .
   calculations.
       perform std-dev
       perform geo-mean
       perform harm-mean
       perform rms
       .
   std-dev.
       compute formatted-data rounded =
           function standard-deviation (data-item (all))
       string "Standard Deviation" formatted-data
           delimited size into output-record
       write output-record
       move space to output-record
       .
   geo-mean.
       perform varying n from 1 by 1
       until n > data-count
           compute fp-item (n) =
               function log10 (data-item (n))
       end-perform
       compute formatted-data rounded =
           10 ** (
               function sum (fp-item (all)) / data-count)
       string "Geometric Mean" formatted-data
           delimited size into output-record
       write output-record
       move space to output-record
       .
   harm-mean.
       perform varying n from 1 by 1
       until n > data-count
           compute fp-item (n) = 1 / data-item (n)
       end-perform
       compute formatted-data rounded =
           data-count / function sum (fp-item (all))
       string "Harmonic Mean" formatted-data
           delimited size into output-record
       write output-record
       move space to output-record
       .

   rms.
       perform varying n from 1 by 1
       until n > data-count
           compute fp-item (n) = data-item (n) ** 2
       end-perform
       compute formatted-data rounded =
           function sqrt (
               (function sum (fp-item (all)) / data-count))
       string "Root Mean Square" formatted-data
           delimited size into output-record
       write output-record
       move space to output-record
       .

Input:

25
34.85
17.1
0.5
107.99

Output:

             25.0000
             34.8500
             17.1000
              0.5000
            107.9900
Standard Deviation             37.1925
Geometric Mean             15.1739
Harmonic Mean              2.3403
Root Mean Square             52.5243