Section Arrays: Reshape#
Adapted from: gjbex/Fortran-MOOC
This program reshapes an array into a matrix in Fortran.#
program reshape_test
    implicit none
    integer, parameter :: nr_values = 12
    integer :: i
    integer, dimension(nr_values) :: values = [ (i, i=1, nr_values) ]
    integer, dimension(2, 5) :: too_small
    integer, dimension(3, 5) :: too_large
    too_small = reshape(values, shape(too_small))
    call print_matrix(too_small)
    too_large = reshape(values, shape(too_large), pad=[-1, -2])
    call print_matrix(too_large)
contains
    subroutine print_matrix(A)
        implicit none
        integer, dimension(:, :), intent(in) :: A
        integer :: i
        do i = 1, size(A, 1)
            print *, A(i, :)
        end do
    end subroutine print_matrix
end program reshape_test
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_Reshape"
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")
           1           3           5           7           9
           2           4           6           8          10
           1           4           7          10          -1
           2           5           8          11          -2
           3           6           9          12          -1
 
    