function [ av, var, xmin, xmax ] = stats ( x, n )

%*****************************************************************************80
%
%% stats() computes statistics for a given array.
%
%  Discussion:
%
%    This procedure computes the average and variance of an array.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    31 March 2013
%
%  Author:
%
%    Original FORTRAN77 version by Barry Brown, James Lovato.
%    MATLAB version by John Burkardt.
%
%  Input:
%
%    real X(N), the array to be analyzed.
%
%    integer N, the dimension of the array.
%
%  Output:
%
%    real AV, the average value.
%
%    real VAR, the variance.
%
%    real XMIN, XMAX, the minimum and maximum entries.
%
  xmin = x(1);
  xmax = x(1);
  total = 0.0;
  for i = 1 : n
    total = total + x(i);
    xmin = min ( xmin, x(i) );
    xmax = max ( xmax, x(i) );
  end

  av = total / n;

  total = 0.0;
  for i = 1 : n
    total = total + ( x(i) - av ) ^ 2;
  end
  var = total / ( n - 1 );

  return
end
