PROGRAM array
    IMPLICIT NONE
    REAL, DIMENSION(3, 4) :: work1, work2 

    work2(1, :) = (/ 2.35, 2.82, 4.55, 7.83 /)
    work2(2, :) = (/ 3.97, 6.75, 7.62, 8.36 /)
    work2(3, :) = (/ 8.97, 0.74, 2.70, 2.49 /)
    CALL scaler(work1, work2)
    PRINT *, work1(1, :)
    PRINT *, work1(2, :)
    PRINT *, work1(3, :)

CONTAINS

    SUBROUTINE scaler (arg1, arg2)
        IMPLICIT NONE
        REAL, DIMENSION(:, :), INTENT(OUT) :: arg1
        REAL, DIMENSION(:, :), INTENT(IN) :: arg2
        REAL, DIMENSION(:), ALLOCATABLE :: temp1, temp2
        INTEGER :: j, k, m, n

        m = UBOUND(arg1, 1)
        n = UBOUND(arg1, 2)
        IF (m /= UBOUND(arg2, 1) .OR. n /= UBOUND(arg2, 2)) THEN
            PRINT *, 'The arrays are incompatible'
            STOP 1
        END IF
        ALLOCATE(temp1(1:UBOUND(arg1, 1)))
        ALLOCATE(temp2(1:UBOUND(arg1, 2)))
        DO j = 1, m
            temp1(j) = sum(arg2(j, :))/n
        END DO
        DO k = 1, n
            temp2(k) = sum(arg2(:, k))/m
        END DO
        DO j = 1, n
            DO k = 1, m
                arg1(k, j) = arg2(k, j) - temp1(k) - &
                    temp2(j) + sum(temp1)/m
            END DO
        END DO
    END SUBROUTINE scaler

END PROGRAM array
