Task: Cumulative standard deviation

C: #include <stdio.h>
#include <stdlib.h>
#include <math.h>

typedef enum Action { STDDEV, MEAN, VAR, COUNT } Action;

typedef struct stat_obj_struct {
   double sum, sum2;
   size_t num;
   Action action; 
} sStatObject, *StatObject;

StatObject NewStatObject( Action action )
{
  StatObject so;

  so = malloc(sizeof(sStatObject));
  so->sum = 0.0;
  so->sum2 = 0.0;
  so->num = 0;
  so->action = action;
  return so;
}
#define FREE_STAT_OBJECT(so) \
   free(so); so = NULL
double stat_obj_value(StatObject so, Action action)
{
  double num, mean, var, stddev;
    
  if (so->num == 0.0) return 0.0;
  num = so->num;
  if (action==COUNT) return num;
  mean = so->sum/num;
  if (action==MEAN) return mean;
  var = so->sum2/num - mean*mean;
  if (action==VAR) return var;
  stddev = sqrt(var);
  if (action==STDDEV) return stddev;
  return 0;
}

double stat_object_add(StatObject so, double v)
{
  so->num++;
  so->sum += v;
  so->sum2 += v*v;
  return stat_obj_value(so, so->action);
}









COBOL: IDENTIFICATION DIVISION.
PROGRAM-ID. run-stddev.
environment division.
input-output section.
file-control.
  select input-file assign to "input.txt"
    organization is line sequential.
data division.
file section.
fd input-file.
  01  inp-record.
    03  inp-fld  pic 9(03).
working-storage section.
01  filler pic 9(01)   value 0.
  88 no-more-input     value 1.
01  ws-tb-data.
  03  ws-tb-size         pic 9(03).
  03  ws-tb-table.
    05  ws-tb-fld     pic s9(05)v9999 comp-3 occurs 0 to 100 times 
        depending on ws-tb-size.
01 ws-stddev       pic s9(05)v9999 comp-3.
PROCEDURE DIVISION.
  move 0 to ws-tb-size 
  open  input input-file
    read input-file
    at end
      set no-more-input to true
    end-read
    perform
      test after
    until no-more-input
      add 1 to ws-tb-size
      move inp-fld to ws-tb-fld (ws-tb-size)
      call 'stddev' using  by reference ws-tb-data 
         ws-stddev
      display  'inp=' inp-fld ' stddev=' ws-stddev
      read input-file at end set no-more-input to true end-read
    end-perform
  close input-file
  stop run.
end program run-stddev.
IDENTIFICATION DIVISION.
PROGRAM-ID. stddev.
data division.
working-storage section.
01 ws-tbx             pic s9(03) comp.
01 ws-tb-work.
  03  ws-sum          pic s9(05)v9999 comp-3 value +0.
  03  ws-sumsq        pic s9(05)v9999 comp-3 value +0.
  03  ws-avg          pic s9(05)v9999 comp-3 value +0.
linkage section.
01  ws-tb-data.
  03  ws-tb-size         pic 9(03).
  03  ws-tb-table.
    05  ws-tb-fld     pic s9(05)v9999 comp-3 occurs 0 to 100 times 
        depending on ws-tb-size.
01  ws-stddev       pic s9(05)v9999 comp-3.
PROCEDURE DIVISION using  ws-tb-data  ws-stddev.
    compute ws-sum = 0
    perform test before varying ws-tbx from 1 by +1 until ws-tbx > ws-tb-size
        compute ws-sum = ws-sum + ws-tb-fld (ws-tbx) 
    end-perform
    compute ws-avg rounded = ws-sum / ws-tb-size
    compute ws-sumsq = 0
    perform test before varying ws-tbx from 1 by +1 until ws-tbx > ws-tb-size
        compute ws-sumsq = ws-sumsq
        + (ws-tb-fld (ws-tbx) - ws-avg) ** 2.0
    end-perform
    compute ws-stddev = ( ws-sumsq / ws-tb-size) ** 0.5 
    goback.
end program stddev.