c-----------------------------------------------------------
c Chapter 24: Invert a Triangular Array(p226)
c-----------------------------------------------------------
c   Name of subroutine: INVERT 
c
c   Algorithm:Invert upper trianular matrix.
c
c   input:    n=10
c   complier: f77 invert_2.f
c-----------------------------------------------------------

	program driver
	parameter (n=10)
	integer  a, ainv
	dimension a(n, n), ainv(n, n)
	integer i,j

	write(6,*) "The input array is:"
	do 3 i=1,n
	   do 2 j=1,n
		if (i.gt.j) go to 1
		a(i,j)=1
		go to 2
1	a(i,j)=0
	go to 2
2	continue
	write(6, 4) (a(i,j), j=1,n)
3	continue

	call invert(n, a, ainv)
	write(6,*) "The inverted array is:"
	do 5 i=1,n
		write(6,4) (ainv(i,j), j=1,n)
4	format(10i3)
5	continue
	end

c-----Subroutine begins here--------------------------------
      subroutine invert (n,a,ainv)
      integer a,ainv,sum
      dimension a(n,n),ainv(n,n)
      j=n
10    i=n
20    sum=0
      if (i.eq.j) sum=1
      k=i+1
25    if (k.gt.j) go to 30
      sum=sum-a(i,k)*ainv(k,j)
      k=k+1
      go to 25
30    ainv (i,j)=sum
      i=i-1
      if (i.gt.0) go to 20
      j=j-1
      if (j.gt.0) go to 10
      return
      end


