! Demo for 2-d array simple manipulation ! Written by siva@cse.iitb.ernet.in on Aug 21,2000 ! Reads in a 2-D matrix and prints the row with biggest sum Program arraydemo IMPLICIT NONE ! must put this first Integer, Dimension(4,3) :: Matrix ! 4 rows and 3 columns Integer :: Row, Col ! counters for DO loop Integer :: Rsum, Maxsum ! keeps sum of rows Integer :: MaxRow ! which row has max sum Integer :: C !used for printing a row using Implied Do. ! Read in the Matrix first Do Row = 1, 4 Do Col = 1, 3 Read (*,*) Matrix(Row,Col) ! 1 number per line. not good! End Do End Do ! compute row sums and find which row is maximum MaxRow = 0 MaxSum = 0 ! start with 0 assuming all matrix elements are positive ! how to do this in general when negative numbers allowed? Do Row = 1, 4 RSum = 0 ! initialize sum of current row to 0 Do Col = 1, 3 RSum = RSum + Matrix(Row,Col) End Do ! now RSum has sum of current row. write it. Note: ***Implied DO used** Write (*,*) 'Row number ', Row, ' is', (Matrix(Row, C), C=1,3), & ' with sum= ', Rsum ! compare with max found so far If (RSum > MaxSum) Then MaxSum = RSum MaxRow = Row End If End Do ! now MaxRow and MaxSum contain the desired answer. Display and end. Write (*,*) 'Biggest row is: ', MaxRow Write (*,*) 'Elements are: ', (Matrix(MaxRow,C), C = 1,3) Write (*,*) 'With sum: ', MaxSum End Program arraydemo