program main implicit none integer :: n,m,i,j,ierr integer, allocatable :: f(:),v(:),g(:) real*8 :: rtmp,itmp,sum_row,sumt real*8, allocatable :: b(:,:),c(:) real*8, allocatable :: d(:) complex(8), allocatable :: a(:,:) !1) do write(*,*) 'Insert n>0' read(*,*) n if (n.gt.0) exit enddo allocate(a(n,n)) allocate(b(n,n)) allocate(c(n)) allocate(d(n)) allocate(f(n)) open(10,file='inputA.dat',status='old',iostat=ierr,err=500) do i=1,n do j=1,n read(10,*) rtmp,itmp a(j,i)=dcmplx(rtmp,itmp) enddo enddo close(10) !2) sumt=1.d0 do i=1,n sum_row=0.d0 do j=1,n sum_row=sum_row+abs(a(i,j)) enddo sumt = sumt*sum_row enddo write(*,*) 'The value is', sumt if (sumt.gt.0.d0.and.sumt.le.4.d0) then do i=1,n do j=1,n b(i,j)=dble(a(i,j)) enddo enddo elseif (sumt.gt.4.d0.and.sumt.le.8.d0) then do i=1,n do j=1,n b(i,j)=aimag(a(i,j)) enddo enddo elseif (sumt.gt.8.d0) then do i=1,n do j=1,n b(i,j)=abs(a(i,j)) enddo enddo endif !3) do i=1,n d(i)=b(i,n-(i-1)) enddo call prod(c,b,d,n) call do_f(f,c,b,n) !4) do write(*,*) 'Insert m>0' read(*,*) m if (m.gt.0) exit enddo call modify_f(m,n,f) !5) allocate(v(m)) open(11,file='inputv.dat',status='old',iostat=ierr,err=500) do i=1,m read(11,*) v(i) enddo close(11) allocate(g(m+n)) do i=1,m g(i)=v(i) enddo do i=1,n g(m+i)=f(i) enddo call sort(g,n,m) !6) open(20,file='a.dat') open(21,file='b.dat') do i=1,n do j=1,n write(20,205) i,j,dble(a(i,j)),aimag(a(i,j)) write(21,200) i,j,b(i,j) enddo enddo close(20) close(21) open(22,file='c.dat') open(23,file='g.dat') do i=1,n write(22,215) i, c(i) enddo do i=1,n+m write(23,210) i, g(i) enddo close(22) close(23) 200 format(I4,I4,F12.6) 205 format(I4,I4,2(F12.6)) 210 format(I4,I4) 215 format(I4,F12.6) 500 if (ierr.ne.0) then write(*,*) 'Error in opening the input file' stop endif deallocate(a) deallocate(b) deallocate(c) deallocate(d) deallocate(f) deallocate(g) deallocate(v) stop end program main subroutine prod(c,b,d,n) implicit none integer, intent(in) :: n real*8, intent(in) :: b(n,n),d(n) real*8, intent(out) :: c(n) integer :: i,j c=0.d0 do i=1,n do j=1,n c(i)=c(i)+b(i,j)*d(j) enddo enddo return end subroutine prod subroutine do_f(f,c,b,n) implicit none integer, intent(in) :: n real*8, intent(in) :: c(n) real*8, intent(in) :: b(n,n) integer, intent(out) :: f(n) integer :: i real*8 :: tmp do i=1,n tmp=b(i,i)-c(i) f(i) = ceiling(tmp) enddo return end subroutine do_f subroutine modify_f(m,n,f) implicit none integer, intent(in) ::m,n integer, intent(inout) :: f(n) integer :: i if (m.ge.n) then do i=1,n/2,2 f(i)=f(i)+(m-n) enddo do i=2,n/2,2 f(i)=f(i)+(n-m) enddo else do i=n/2+1,n f(i)=f(i)+(n+m) enddo endif return end subroutine modify_f subroutine sort(g,n,m) implicit none integer, intent(in) :: n,m integer, intent(inout) :: g(n+m) integer :: i,j,tmp do i=1,n+m do j=2,n+m-(i-1) if (g(j-1).gt.g(j)) then tmp=g(j-1) g(j-1)=g(j) g(j)=tmp endif enddo enddo return end subroutine sort