{Statistical function library for Pascal}

MODULE STATLIB;
{$M MINMAXI}
{$M MEANI}
{$M SIGMAI}
{$M MINMAXR}
{$M MEANR}
{$M SIGMAR}
{$M *}

TYPE
  NATURAL = 0..MAXINT;

PROCEDURE MINMAX_I
  (VAR X:ARRAY[LOW..HIGH:NATURAL]OF INTEGER;N:INTEGER;VAR MIN,MAX:INTEGER);
VAR
  I:NATURAL;
BEGIN
  I:=LOW; {starting point of array}
  MIN:=32767;
  MAX:=-32767; {limit values}
    REPEAT
      IF X[I] < MIN THEN
	MIN:=X[I]
      ELSE
	IF X[I] > MAX THEN
	  MAX:=X[I];
      I:=I+1
    UNTIL I > N
END;

PROCEDURE MEAN_I
  (VAR X:ARRAY[LOW..HIGH:NATURAL]OF INTEGER;N:INTEGER;VAR MEAN:REAL);
VAR
  J:NATURAL;
  SUM:REAL;
BEGIN
  J:=LOW;
  SUM:=0.0;
    REPEAT
      SUM:=SUM+X[J];
      J:=J+1
    UNTIL J > N;
  MEAN:=SUM/N
END;

PROCEDURE SIGMA_I
  (VAR X:ARRAY[LOW..HIGH:NATURAL]OF INTEGER;N:INTEGER;VAR SIGMA:REAL);
VAR
  K:NATURAL;
  SUM,AVE:REAL;
BEGIN
  MEAN_I(X,N,AVE);
  K:=LOW;
  SUM:=0.0;
    REPEAT
      SUM:=SUM+SQR(X[K]-AVE);
      K:=K+1
    UNTIL K > N;
  SIGMA:=SQRT(SUM/N)
END;

PROCEDURE MINMAX_R
  (VAR X:ARRAY[LOW..HIGH:NATURAL]OF REAL;N:INTEGER;VAR MIN,MAX:REAL);
VAR
  L:NATURAL;
BEGIN
  L:=LOW; {starting point of array}
  MIN:=10.0E+17;
  MAX:=10.0E-17; {limit values}
    REPEAT
      IF X[L] < MIN THEN
	MIN:=X[L]
      ELSE
	IF X[L] > MAX THEN
	  MAX:=X[L];
      L:=L+1
    UNTIL L > N
END;

PROCEDURE MEAN_R
  (VAR X:ARRAY[LOW..HIGH:NATURAL]OF REAL;N:INTEGER;VAR MEAN:REAL);
VAR
  M:NATURAL;
  SUM:REAL;
BEGIN
  M:=LOW;
  SUM:=0.0;
    REPEAT
      SUM:=SUM+X[M];
      M:=M+1
    UNTIL M > N;
  MEAN:=SUM/N
END;

PROCEDURE SIGMA_R
  (VAR X:ARRAY[LOW..HIGH:NATURAL]OF REAL;N:INTEGER;VAR SIGMA:REAL);
VAR
  Q:NATURAL;
  SUM,AVE:REAL;
BEGIN
  MEAN_R(X,N,AVE);
  Q:=LOW;
  SUM:=0.0;
    REPEAT
      SUM:=SUM+SQR(X[Q]-AVE);
      Q:=Q+1
    UNTIL Q > N;
  SIGMA:=SQRT(SUM/N)
END;

MODEND.
 