function z = r8vec_smooth ( n, x, s )

%*****************************************************************************80
%
%% r8vec_smooth smooths an R8VEC.
%
%  Discussion:
%
%    An R8VEC is a vector of R8's.
%
%    Except for the beginning and ending entries, the vector values
%    are replaced by averages of 2*S+1 neighbors.
%
%  Example:
%
%    S = 2
%
%    Z(1)   =                     X(1)
%    Z(2)   = (          X(1)   + X(2)   + X(3) ) / 3
%    Z(3)   = ( X(1)   + X(2)   + X(3)   + X(4)   + X(5) ) / 5
%    Z(4)   = ( X(2)   + X(3)   + X(4)   + X(5)   + X(6) ) / 5
%    ...
%    Z(N-2) = ( X(N-4) + X(N-3) + X(N-2) + X(N-1) + X(N) ) / 5
%    Z(N-1) =          ( X(N-2) + X(N-1) + X(N) ) / 3
%    Z(N) =                       X(N)
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    23 April 2019
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer N, the dimension of X.
%
%    real X(N), the vector to be smoothed.
%
%  Output:
%
%    real Z(N), the smoothed vector.
%
  z = zeros ( n, 1 );

  for j = 1 : s
    z(j) = sum ( x(1:2*j-1) ) / ( 2 * j - 1 );
  end

  for j = s + 1 : n - s
    z(j) = sum ( x(j-s:j+s) ) / ( 2 * s + 1 );
  end

  for j = s : -1 : 1
    z(n+1-j) = sum ( x(n+1-(2*j-1):n) ) / ( 2 * j - 1 );
  end

  return
end
