program main implicit none real*8, allocatable :: a(:,:), b(:,:) integer, allocatable :: c(:) real*8 :: suma, sumb, diag integer :: n, l, i, j, k, id !1) open(10,file='input.dat') read(10,*) n,l if (l.ge.n) then write(*,*) 'n must be greater than l' stop endif allocate(a(n,l)) do i=1,n read(10,*) (a(i,j), j=1,l) enddo close(10) !2) allocate(b(l,l)) b(:,:)=a(1:l,:) !3) suma=0.d0 do i=l+1,n do j=1,l suma=suma+a(i,j) enddo enddo sumb=0.d0 do i=1,l do j=1,l,2 sumb=sumb+b(i,j) enddo enddo if (suma.ge.sumb) then write(*,*) 'Case a', suma, sumb diag=0.d0 do i=1,l diag=diag+b(i,i) enddo id=abs(int(diag)) if (id.eq.0) then write(*,*) 'Array length equal to zero' else allocate(c(id)) do i=1,id c(i)=i+int(a(1,1)) enddo endif else write(*,*) 'Case b', suma, sumb id=2*l allocate(c(id)) k=0 do j=1,2 do i=1,l k=k+1 c(k)=b(j,i) enddo enddo endif !4) if (id.gt.0) then open(10,file='c.dat') do i=1,id write(10,100) i,c(i) enddo endif open(11,file='b.dat') do i=1,l do j=1,l write(11,101) i,j,b(i,j) enddo enddo close(11) 100 format(I5,I5) 101 format(I5,I5,F12.5) deallocate(a) deallocate(b) if (id.gt.0) deallocate(c) stop end program main