function a = r8vec_insert ( n, a, pos, value )

%*****************************************************************************80
%
%% R8VEC_INSERT inserts a value into an R8VEC.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    23 April 2005
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer N, the dimension of the array on input.
%
%    real A(N), the array.  
%
%    integer POS, the position to be assigned the new entry.
%    1 <= POS <= N+1.
%
%    real VALUE, the value to be inserted.
%
%  Output:
%
%    real A(N+1), the array, with VALUE inserted at the POS-th position.
%
  if ( pos < 1 || n + 1 < pos )

    fprintf ( 1, '\n' );
    fprintf ( 1, 'R8VEC_INSERT - Fatal error!\n' );
    fprintf ( 1, '  Illegal insertion position = %d\n', pos );
    error ( 'R8VEC_INSERT - Fatal error!' );

  else

    for i = n+1 : -1 : pos+1
      a(i) = a(i-1);
    end

    a(pos) = value;

  end

  return
end
