FORTRAN 90 Lecturer : Rafel Hekmat Hameed University of Babylon Subject : Fortran 90 College of Engineering Year : Second B.Sc. Mechanical Engineering Dep. Examples about Arrays EXAMPLE 1 You have matrix a with shape (/3,5/) find the largest element in the matrix and then find the summation of the row and column containing the largest element, use format statement to print your results. Program aa Implicit none Integer, parameter :: n=3, m=5 Integer, dimension (n,m):: a Integer::I,j,s1,s2,j1,i1,maxa Read(*,*) ((a(I,j),j=1,m),i=1,n) Maxa=a(1,1) Do i=1,n Do j=1,m If ( a(I,j) .gt.maxa)then maxa=a(I,j) i1=i j1=j endif enddo do j=1,m ϭ s1=s1+a(i1,j) enddo enddo do i=1,n s2=s2+a(I,j1) enddo write(*,40) ((a(i,j),j=1,m),i=1,n) 40 format( 5(2x,i3)) Write(*,50) maxa,s1,s2 50 format(2x,"maximum element=",i3,2x,"summation of row=",i5,2x,"summation& & of column=",2x,i5) end EXAMPLE 2 Write a Fortran90 program to computes these two quantities from (10) numbers; [read the numbers from output file]: ୬ ͳ ത ൌ ୧ୀଵ ୬ ͳ ൌ ሺ െ തሻ െͳ ଶ Program bb ୧ୀଵ Implicit none Integer, parameter::n=10 Real, dimension (n)::x real:: xbar=0 ,std=0 integer::i open(1,file='data.dat') do i=1,n read(1,*) x(i) xbar=xbar+x(i) enddo Ϯ xbar=xbar/n do i=1,n std=std+(x(i)-xbar) enddo std=sqrt(std/(n-1)) write(*,20)'mean=',xbar ; 20 format(2x,a,f12.8) write(*,20)'std=',std end EXAMPLE 3 Array of rank 2 its shape (/3,4/) Write a program to ascending this matrix as the sum of each row. Ͷ Ͷͻ ͵Ͷ ͺ ͻ ͳ Ͷͷ Ͷ ֜ ͳʹ ʹͷ ͵Ͷ ൩ ͳʹ ʹͷ ͵Ͷ ͺ൩ ͺ ͻ ͳ Ͷͷ Ͷ Ͷͻ ͵Ͷ ͻͶ Program aw Implicit none Integer, parameter:: n=3,m=4 Integer, dimension (n,m+1)::a real,dimension (n)::s integer:: i,j,z Read(*,*) ((a(I,j),j=1,m),i=1,n) Do i=1,n S(i)=0 Do j=1,m S(i)=s(i)+a(i,j) Enddo ; enddo Do i=1,n A( i,m+1)=s(i) enddo do i=1,n-1 ϯ Do j=1,m+1 If (a( I, m+1) .gt. a(i+1,m+1)) then Z=a(I,j) A(I,j) = a(i+1, j) A( i+1 ,j) = z Endif Enddo;enddo Write(*,67) ((a(I,j), j=1,m+1),i=1,n) 67 format ( 5(2x,i4)) End ϰ