function x=gebs6_1(A,b,verbose)
%%   function x=gebs6_1(A,b,verbose);
%% Gaussian Elimination with Backward Substitution On Square 
%% Systems Ax=b (pivot only to first nonzero entry in the 
%% column)
%% 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 leave blank 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;

% Make the augmented matrix;
A=[A,b];

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

%%STEP 1
for i=1:n-1
%%%%%Step 2
  if flag==0
     I=find(abs(A(i:n,i))>0);
     if isempty(I)==1
        flag=1;
     else  
%%%%%%%%Step 3 (interchange rows if necessary)
        p=I(1)+i-1;
          if p>i;
              B=A(i,:);
              A(i,:)=A(p,:);
              A(p,:)=B;clear B;
           end;
     end;
%%%%%%Step 4 (perform the row operations)
       if flag==0;
         for j=i+1:n
            m=A(j,i)/A(i,i);
            A(j,i:n+1)=A(j,i:n+1)-m*A(i,i:n+1);clear m;
         end;
         if verbose==1;
               q=int2str(p);
               col=int2str(i);
               ['column','  ',col,',  ','pivot row','  ',q]
               A
         end;
       end;
   end;
end;
%%%%STEP 7
if flag==0;
   if A(n,n)==0
    flag=1;
    i=n+1;
   end;
end;
%%%Step 8  (begin backward substitution)
if flag==0;
  x(n,1)=A(n,n+1)/A(n,n);
%%%%Step 9
   for i=n-1:-1:1;
   x(i,1)=(A(i,n+1)-sum(x(i+1:n,1)'.*A(i,i+1:n)))/A(i,i); 
   end;
else
  x='no unique solution may exist';
end;
