function [ Ab, p ] = pivot ( j )

%*****************************************************************************80
%
%% pivot() finds the pivot row for variable j.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    01 January 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer j, the variable for which a pivot is sought.
%
%  Output:
%
%    real Ab(m,n+k), the augmented matrix after pivoting.
%
%    integer p, the row that was chosen for the pivot.
%
  global Ab
  global k
  global m
  global n

  [ value, p ] = max ( abs ( Ab(j:n,j) ) );

  if ( value == 0.0 )
    fprintf ( 'pivot - Fatal error!\n' );
    fprintf ( '  No nonzero pivot could be found.\n' );
    error ( 'pivot - Fatal error!' );
  end

  p = p + j - 1;
  if ( p ~= j )
    t       = Ab(j,:);
    Ab(j,:) = Ab(p,:);
    Ab(p,:) = t;
  end

  return
end

