Section Arrays: Max Array#

Adapted from: gjbex/Fortran-MOOC

This program demonstrates the maxval and maxloc intrinsic functions in Fortran.#

program max_array
    implicit none
    integer, dimension(3, 4) :: A
    integer :: i, j
    character(len=10), parameter :: FMT = '(A, 4I13)'

    A = reshape (  [ &
                    ( &
                      ((i - 1)*size(A, 2) + j, &
                        j=1,size(A, 2)), &
                        i=1,size(A, 1) &
                    ) &
                  ], &
                  shape(A) &
                ) - 8

    ! Print out array
    do i = 1, size(A, 1)
        print *, A(i, :)
    end do

    print *

    print FMT, 'maximum =                       ', maxval(A)
    print FMT, 'row maximum =                   ', maxval(A, dim=1)
    print FMT, 'column maximum =                ', maxval(A, dim=2)
    print FMT, 'maximum odd elment =            ', maxval(A, mask=mod(A, 2) /= 0)
    print FMT, 'row maximum negative element =  ', maxval(A, dim=2, mask=A < 0)
    print FMT, 'maxloc (row, col) =             ', maxloc(A)
    print FMT, 'column maxloc (columns) =       ', maxloc(A, dim=2)
end program max_array

The above program is compiled and run using Fortran Package Manager (fpm):

Build the Program using FPM (Fortran Package Manager)#

import os
root_dir = os.getcwd()
code_dir = root_dir + "/" + "Fortran_Code/Section_Arrays_Max_Array"
os.chdir(code_dir)
build_status = os.system("fpm build 2>/dev/null")

Run the Program using FPM (Fortran Package Manager)#

exec_status = os.system("fpm run 2>/dev/null")
          -7          -4          -1           2
          -6          -3           0           3
          -5          -2           1           4

maximum =                                   4
row maximum =                              -5           -2            1            4
column maximum =                            2            3            4
maximum odd elment =                        3
row maximum negative element =             -1           -3           -2
maxloc (row, col) =                         3            4
column maxloc (columns) =                   4            4            4