      program Gauss
      implicit real*8 (a-h,o-z)
      parameter (N=3)
      dimension a(N,N),b(N),x(N)
      
*************************************************************
*     1o MEROS THS ASKHSHS: APLH GRAFH TOY PROGRAMMATOS     *
*************************************************************
      a11= 3d0
      a12=-1d0
      a13= 2d0
      a21= 1d0
      a22= 2d0
      a23= 3d0
      a31= 2d0
      a32=-2d0
      a33=-1d0
      
      b1 =12d0
      b2 =11d0
      b3 = 2d0
      print*, 'Proto meros tou programmatos:'
      print*, 'Oi pinakes A(3x3) kai B(3x1):'      
      print '(4f7.3)',a11,a12,a13,b1
      print '(4f7.3)',a21,a22,a23,b2
      print '(4f7.3)',a31,a32,a33,b3
      print*
      
*     BHMA 1o - MHDENIZW THN 1H STHLH
*     2h grammh
      factor=a21/a11
      a21=a11*factor-a21        !Mhdenizetai
      a22=a12*factor-a22        !Allazei
      a23=a13*factor-a23        !Allazei
      b2 =b1 *factor-b2         !Allazei
*     3h grammh
      factor=a31/a11
      a31=a11*factor-a31        !Mhdenizetai
      a32=a12*factor-a32        !Allazei
      a33=a13*factor-a33        !Allazei
      b3 =b1 *factor-b3         !Allazei
      
      print*, 'Meta to midenismo tis protis stilis: '
      print '(4f7.3)',a11,a12,a13,b1
      print '(4f7.3)',a21,a22,a23,b2
      print '(4f7.3)',a31,a32,a33,b3
      print*
      
*     BHMA 2o - MHDENIZW THN 2H STHLH
*     3h grammh
      factor=a32/a22
      a31=a21*factor-a31        !Mhdenizetai
      a32=a22*factor-a32        !Mhdenizetai
      a33=a23*factor-a33        !Allazei
      b3 =b2 *factor-b3         !Allazei
      
      print*, 'Meta to midenismo tis defteris stilis:'
      print '(4f7.3)',a11,a12,a13,b1
      print '(4f7.3)',a21,a22,a23,b2
      print '(4f7.3)',a31,a32,a33,b3
      print*, 'O pinakas A exei ginei ano trigonikos'     
      print*
      
*     LYSH TOY SYSTHMATOS
      x3= b3               /a33
      x2=(b2       -a23*x3)/a22
      x1=(b1-a12*x2-a13*x3)/a11

      print*, 'H lisi tou sistimatos:'       
      print'(f7.3)',x1
      print'(f7.3)',x2
      print'(f7.3)',x3
      print*, 'Press Enter to continue'
      pause
      print*
        
**************************************************
*     2o MEROS THS ASKHSHS: GRAFH ME PINAKES     *
**************************************************
      a(1,1)= 3d0
      a(1,2)=-1d0
      a(1,3)= 2d0
      a(2,1)= 1d0
      a(2,2)= 2d0
      a(2,3)= 3d0
      a(3,1)= 2d0
      a(3,2)=-2d0
      a(3,3)=-1d0
      
      b(1) =12d0
      b(2) =11d0
      b(3) = 2d0
      print*, 'Deftero meros tou programmatos'
      print*, 'Oi pinakes A(3x3) kai B(3x1)' 
      print '(4f7.3)',a(1,1),a(1,2),a(1,3),b(1)
      print '(4f7.3)',a(2,1),a(2,2),a(2,3),b(2)
      print '(4f7.3)',a(3,1),a(3,2),a(3,3),b(3)
      print*
      
*     BHMA 1o - MHDENIZW THN 1H STHLH
*     2h grammh
      factor=a(2,1)/a(1,1)
      a(2,1)=a(1,1)*factor-a(2,1) !Mhdenizetai
      a(2,2)=a(1,2)*factor-a(2,2) !Allazei
      a(2,3)=a(1,3)*factor-a(2,3) !Allazei
      b(2)  =b(1)  *factor-b(2) !Allazei
*     3h grammh
      factor=a(3,1)/a(1,1)
      a(3,1)=a(1,1)*factor-a(3,1) !Mhdenizetai
      a(3,2)=a(1,2)*factor-a(3,2) !Allazei
      a(3,3)=a(1,3)*factor-a(3,3) !Allazei
      b(3)  =b(1)  *factor-b(3) !Allazei

      print*, 'Meta to midenismo tis protis stilis'      
      print '(4f7.3)',a(1,1),a(1,2),a(1,3),b(1)
      print '(4f7.3)',a(2,1),a(2,2),a(2,3),b(2)
      print '(4f7.3)',a(3,1),a(3,2),a(3,3),b(3)
      print*
      
*     BHMA 2o - MHDENIZW THN 2H STHLH
*     3h grammh
      factor=a(3,2)/a(2,2)
      a(3,1)=a(2,1)*factor-a(3,1) !Mhdenizetai
      a(3,2)=a(2,2)*factor-a(3,2) !Mhdenizetai
      a(3,3)=a(2,3)*factor-a(3,3) !Allazei
      b(3)  =b(2)  *factor-b(3) !Allazei

      print*, 'Meta to midenismo tis defteris stilis:'      
      print '(4f7.3)',a(1,1),a(1,2),a(1,3),b(1)
      print '(4f7.3)',a(2,1),a(2,2),a(2,3),b(2)
      print '(4f7.3)',a(3,1),a(3,2),a(3,3),b(3)
      print*, 'O pinakas A exei ginei ano trigonikos' 
      print*
      
*     LYSH TOY SYSTHMATOS
      x(3)= b(3)                         /a(3,3)
      x(2)=(b(2)            -a(2,3)*x(3))/a(2,2)
      x(1)=(b(1)-a(1,2)*x(2)-a(1,3)*x(3))/a(1,1)
      
      print*, 'H lisi tou sistimatos:'  
      print'(f7.3)',x(1)
      print'(f7.3)',x(2)
      print'(f7.3)',x(3)
      print*, 'Press Enter to continue'
      pause
      print*
      
*******************************************
*     3o MEROS THS ASKHSHS: GENIKEYSH     *
*******************************************
      a(1,1)= 3d0
      a(1,2)=-1d0
      a(1,3)= 2d0
      a(2,1)= 1d0
      a(2,2)= 2d0
      a(2,3)= 3d0
      a(3,1)= 2d0
      a(3,2)=-2d0
      a(3,3)=-1d0
      
      b(1) =12d0
      b(2) =11d0
      b(3) = 2d0
      print*, 'Trito meros tou programmatos'      
      print*, 'Oi pinakes A(3x3) kai B(3x1)'
      print '(4f7.3)',a(1,1),a(1,2),a(1,3),b(1)
      print '(4f7.3)',a(2,1),a(2,2),a(2,3),b(2)
      print '(4f7.3)',a(3,1),a(3,2),a(3,3),b(3)
      print*
      
      do k=1,N-1
         do i=k+1,N
            factor=a(i,k)/a(k,k)
            do j=k,N
               a(i,j)=a(k,j)*factor-a(i,j)
            enddo
            b(i)=b(k)*factor-b(i)
         enddo
         print*, 'Oi pinakes A kai B:'
         print '(4f7.3)',a(1,1),a(1,2),a(1,3),b(1)
         print '(4f7.3)',a(2,1),a(2,2),a(2,3),b(2)
         print '(4f7.3)',a(3,1),a(3,2),a(3,3),b(3)
         print*
      enddo
      print*, 'O pinakas A exei ginei ano trigwnikos'      
      x(N)=b(N)/a(N,N)
      do k=N-1,1,-1
         sum=b(k)
         do j=k+1,N
            sum=sum-x(j)*a(k,j)
         enddo
         x(k)=sum/a(k,k)
      enddo
      
      print*, 'H lisi tou sistimatos:'      
      print'(f7.3)',x(1)
      print'(f7.3)',x(2)
      print'(f7.3)',x(3)
      print*, 'Press Enter to exit'
      pause
      end
