function x=gescp6_3(A,b,verbose);
%%   function x=gescp6_3(A,b,verbose);
%%% Gaussian Elimination with Scaled Column Pivoting to 
%%  solve system Ax=b
%% INPUT:
%%   A - coefficient matrix of the system of linear equations
%%   b - Right hand side of the system as a column vector (e.g. 
%%   [1;2;3;4;5])
%%   verbose - Put 1 if you want the result of each main step 
%%   (for Homework)
%%           - Put 0 or only two arguments if you only want the 
%%             solution
%% OUTPUT:
%%   x - solution vector as a column vector
%%   'no solution exists' - if pivoting is not possible
%%   IF verbose==1, the intermediate augmented matrices are given

if nargin <3  % if only two arguments given, then verbose is turned off
  verbose=0;
end;


%%Determine the size of the system
n=length(A(:,1));

%%STEP 1
        NROW=1:n;
        x=zeros(n,1);
        flag=0;

Scale=max(abs(A))'; %%This gives a vector of the maximums of each row
      
if min(Scale)==0 %% whether there is a row of zeros in coefficient
		 %% matrix
        flag=1;
end
% Make the augmented matrix;

A=[A,b];

if flag==0;
   for i=1:n-1
         %%Determine the Max scaled quantity in the column on 
         %%and below the diagonal
     M=max(abs(A(NROW(i:n),i))./Scale(NROW(i:n)));
         %% If max is zero, then no solution
         %% otherwise, find first place where max is attained
     if M==0;
        flag=1;
     else  
        I=find(abs(A(NROW(i:n),i))./Scale(NROW(i:n))==M);
         %%(interchange rows if necessary)
            p=I(1)+i-1;
            if p>i
              NCOPY=NROW(i);
              NROW(i)=NROW(p);
              NROW(p)=NCOPY;
            end;
     end;
     if flag==0;
       for j=i+1:n
         m(NROW(j),i)=A(NROW(j),i)/A(NROW(i),i);
         A(NROW(j),i:n+1)=A(NROW(j),i:n+1)-m(NROW(j),i)*A(NROW(i),i:n+1);
       end;
       if verbose==1;
          p=int2str(p);
          col=int2str(i);
         ['column','  ',col,',  ','pivot row','  ',p]
         A(NROW,:)
       end;
     end;
   end;
end;
if flag==0;
   if A(NROW(n),n)==0
    flag=1;
   end;
end;
%%begin backward substitution
if flag==0;
  x(n,1)=A(NROW(n),n+1)/A(NROW(n),n);
   for i=n-1:-1:1;
    x(i,1)=(A(NROW(i),n+1)-sum(x(i+1:n,1)'.*A(NROW(i),i+1:n)))/A(NROW(i),i); 
   end;
else
    x='no unique solution may exist';
end;
