PROGRAM ROW_ADD
! This program adds each row of a matrix A and returns the result in an array B.
! All indices of A and B start at 1. The rth element of B will have the sum of
! the rth row of A.
IMPLICIT NONE
INTEGER :: M, N, I, K
REAL, DIMENSION(:,:), ALLOCATABLE :: A
REAL, DIMENSION(:), ALLOCATABLE :: B
! Now read number of rows and number of columns of A.
PRINT *, "Enter number of rows followed by number of columns:"
READ *, M, N
! Now allocate memory for matrix A and array B:
ALLOCATE(A(M,N))
ALLOCATE(B(M))
! Now read elements of A.
PRINT *, "Enter elements of A row by row:"
DO I=1, M
READ *, (A(I,K), K=1, N)
END DO
! Note you can implement the above DO loop using other ways. For example,
! you can replace the above by:
! READ *, ((A(I,K), K=1, N), I=1, M) .
! A similar thing can be said about all DO loops below.
!====================
!Finding the sum of each row of A starts here.
B=0 !You can remove this statement.
DO I=1, M
DO K=1, N
B(I)=B(I)+A(I,K)
END DO
END DO
!Finding the sum of each row of A ends here.
!====================
! Now we print A and then B:
PRINT *, "Matrix A is:"
DO I=1, M
PRINT *, (A(I,K), K=1, N)
END DO
PRINT *
PRINT *, "Sum of the rows:"
PRINT *, B
! You use DEALLOCATE and ALLOCATE only for run-time arrays.
DEALLOCATE(A, B)
END PROGRAM ROW_ADD