Section Arrays: Identity#

Adapted from: gjbex/Fortran-MOOC

This program demonstrates reading in command line arguments in Fortran.#

program identity
    use, intrinsic :: iso_fortran_env, only : error_unit
    implicit none
    integer :: n, i, istat
    real, dimension(:, :), allocatable :: A

    n = get_size()
    allocate(A(n, n), stat=istat)
    if (istat /= 0) then
        write (unit=error_unit, fmt='(A)') &
            'error: can not allocate array'
        stop 3
    end if
    A = eye(n)
    do i = 1, size(A, 1)
        print *, A(i, :)
    end do
    deallocate(A)

contains

    function get_size() result(n)
        use, intrinsic :: iso_fortran_env, only : error_unit
        implicit none
        integer :: n
        integer :: istat
        character(len=1024) :: buffer, msg

        if (command_argument_count() /= 1) then
            write (unit=error_unit, fmt='(A)') &
                'error: expecting an integer argument, size of array'
            stop 1
        end if
        call get_command_argument(1, buffer)
        read (buffer, fmt=*, iostat=istat, iomsg=msg) n
        if (istat /= 0) then
            write (unit=error_unit, fmt='(2A)') &
                'error: ', msg
            stop 2
        end if
    end function get_size

    function eye(n) result(matrix)
        implicit none
        integer, value :: n
        real, dimension(n, n) :: matrix
        integer :: i

        matrix = 0.0
        do i = 1, size(A, 1)
            matrix(i, i) = 1.0
        end do
    end function eye

end program identity

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_Identity"
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 -- 3 2>/dev/null")
   1.00000000       0.00000000       0.00000000    
   0.00000000       1.00000000       0.00000000    
   0.00000000       0.00000000       1.00000000    
exec_status = os.system("fpm run -- 5 2>/dev/null")
   1.00000000       0.00000000       0.00000000       0.00000000       0.00000000    
   0.00000000       1.00000000       0.00000000       0.00000000       0.00000000    
   0.00000000       0.00000000       1.00000000       0.00000000       0.00000000    
   0.00000000       0.00000000       0.00000000       1.00000000       0.00000000    
   0.00000000       0.00000000       0.00000000       0.00000000       1.00000000