This may be one of the last programs I ever write in Cobol. Now that I won’t be teaching legacy languages anymore, I won’t be spending any time with Cobol (or Ada). This program reads in a file of numbers and calculates a bunch of statistics: mean, standard deviation, geometric mean, harmonic mean, and root mean square.
identification division.
program-id. problems.
environment division.
input-output section.
file-control.
select input-file assign to dynamic fname-inp
organization is line sequential.
data division.
file section.
fd input-file.
01 sample-input pic x(80).
working-storage section.
01 array-area.
02 x pic s9(6)v9(2) usage is computational-3
occurs 1000 times.
01 input-value.
02 in-x pic s9(6)v9(2).
02 filler pic x(72).
77 fname-inp pic x(30).
77 feof pic a(1).
77 n pic 9999 value 0.
77 sum-of-x-sqr pic 9(10)v9(2).
77 sum-of-x pic s9(10)v9(2).
77 sum-of-x-mul pic s9(20)v9(6).
77 temp pic s9(20)v9(6).
77 sum-of-x-inv pic s9(10)v9(6).
77 sum-rms pic s9(20)v9(6).
77 mean pic s9(10)v9(2).
77 stdev pic s9(10)v9(2).
77 geom pic s9(10)v9(2).
77 harm pic s9(10)v9(2).
77 rms pic s9(10)v9(2).
77 i pic 9(6).
01 out-results-1.
02 filler pic x(20) value "Mean = ".
02 out-mean pic -(6)9.9(2).
01 out-results-2.
02 filler pic x(20) value "Standard dev. = ".
02 out-sd pic -(6)9.9(2).
01 out-results-3.
02 filler pic x(20) value "Geometric mean = ".
02 out-gm pic -(6)9.9(2).
01 out-results-4.
02 filler pic x(20) value "Harmonic mean = ".
02 out-hm pic -(6)9.9(2).
01 out-results-5.
02 filler pic x(20) value "Root mean square = ".
02 out-rms pic -(6)9.9(2).
procedure division.
display "Input data filename: "
accept fname-inp.
open input input-file.
move 0 to sum-of-x.
move 0 to sum-of-x-sqr.
move 0 to sum-of-x-mul.
move 0 to sum-of-x-inv.
move 0 to sum-rms.
perform input-loop until feof="Y".
display "STATISTICS CALCULATIONS".
close input-file.
perform calc-mean.
perform calc-sd.
perform calc-gm.
perform calc-hm.
perform calc-rms.
perform finish.
input-loop.
read input-file into input-value
at end move 'Y' to feof
not at end
add 1 to n
move in-x to x(n)
display x(n)
end-read.
calc-mean.
perform sum-array varying i from 1 by 1
until i is greater than n.
compute mean = sum-of-x / n.
move mean to out-mean.
display out-results-1.
sum-array.
compute sum-of-x = sum-of-x + x(i).
calc-sd.
perform sum-array2 varying i from 1 by 1
until i is greater than n.
compute stdev rounded = (sum-of-x-sqr / n) ** 0.5.
move stdev to out-sd.
display out-results-2.
sum-array2.
compute sum-of-x-sqr = sum-of-x-sqr + (x(i) - mean) ** 2.
calc-gm.
perform sum-array3 varying i from 1 by 1
until i is greater than n.
compute geom = 10 ** (sum-of-x-mul / n).
move geom to out-gm.
display out-results-3.
sum-array3.
compute temp = function log10(x(i)).
compute sum-of-x-mul = sum-of-x-mul + temp.
display i, " ", sum-of-x-mul.
calc-hm.
perform sum-array4 varying i from 1 by 1
until i is greater than n.
compute harm = n / sum-of-x-inv.
move harm to out-hm.
display out-results-4.
sum-array4.
compute sum-of-x-inv = sum-of-x-inv + (1.0/x(i)).
calc-rms.
perform sum-array5 varying i from 1 by 1
until i is greater than n.
compute rms = (sum-rms/n) ** 0.5.
move rms to out-rms.
display out-results-5.
sum-array5.
compute sum-rms = sum-rms + (x(i) * x(i)).
finish.
stop run.