PROGRAM Finding_Row_Maxima_2
!-----------------------------------------------------------------------
! Program to read an array of numbers and to find the maximum value in
! each row using the subroutine FindMaxima.  Identifiers used are:
!   IntArray      : two-dimensional allocatable array of numbers
!   NumRows       : number of rows in IntArray
!   NumColumns    : number of columns in IntArray
!   FindRowMaxima : subroutine to find the row maxima
!   I, J          : subscripts
!
! Input:  NumRows, NumColumns, and IntArray
! Output: The maximum value in each row (using FindRowMaxima)
!-----------------------------------------------------------------------

  IMPLICIT NONE
  INTEGER, DIMENSION(:,:), ALLOCATABLE :: IntArray
  INTEGER :: NumRows, NumColumns
  INTEGER :: I, J

  WRITE (*, '(1X, A)', ADVANCE = "NO") &
        "How many rows and how many columns are in the array? "
  READ *, NumRows, NumColumns

  ALLOCATE(IntArray(NumRows, NumColumns))

  PRINT *, "Enter the", NumRows, "", NumColumns, "array of integers &
           &in a rowwise manner:"
  READ *, ((IntArray(I,J), J = 1, NumColumns), I = 1, NumRows)
  
  CALL FindRowMaxima(IntArray)
  STOP

CONTAINS

  !-FindRowMaxima-------------------------------------------------------
  ! Subroutine to find and display the maximum value in each row of a
  ! Rows X Cols array Table of integers.  Local variables used are:
  !   I    : subscript
  !   RowMax : one-dimensional array of row maxima
  !
  ! Accepts: Array Table, Rows, Cols
  ! Output:  The maximum value in each row of Table
  !---------------------------------------------------------------------
  
  SUBROUTINE FindRowMaxima(Table)
  
    INTEGER, DIMENSION(:,:), INTENT(IN) :: Table
    INTEGER, DIMENSION(SIZE(Table,1)) :: RowMax
    INTEGER :: I
  
    RowMax = MAXVAL(Table, 2)  ! Find max values along dimension 2
    DO I = 1, SIZE(table, 1)
       PRINT *, "Maximum entry in row", I, "is", RowMax(I)
    END DO
    RETURN
  END SUBROUTINE FindRowMaxima
  
END PROGRAM Finding_Row_Maxima_2
