Adapted from: “Guide to Fortran 2008 Programming” by Walter S. Brainerd (Springer 2015)
Program to demonstrate the use of the elsewhere construct.¶
program elsewhere_example
implicit none
integer, parameter :: n = 15
integer, dimension(n, n) :: key
integer :: i, j
real, dimension(n, n) :: a
call random_number(a)
do i = 1, n
do j = 1, n
if (i > j) then
! Put negative numbers below the diagonal
a(i, j) = -a(i, j) - 2.0
else if (i < j) then
! Put positive numbers above the diagonal
a(i, j) = a(i, j) + 2.0
else
! Put zeros on the diagonal
a(i, j) = 0.0
end if
end do
end do
where (a > 0)
key = 1
elsewhere (a < 0)
key = -1
elsewhere
key = 0
end where
write (*, "(15f5.1)") (a(i,:), i=1,n)
print *
write (*, "(15i5)") (key(i,:), i=1,n)
end program elsewhere_exampleThe above program is compiled and run using Fortran Package Manager (fpm):
import os
root_dir = os.getcwd()code_dir = root_dir + "/" + "Fortran_Code/Section_4_1_Elsewhere_Example"os.chdir(code_dir)build_status = os.system("fpm build 2>/dev/null")exec_status = os.system("fpm run 2>/dev/null") 0.0 2.9 2.7 2.0 2.7 2.9 3.0 2.6 2.9 2.3 2.7 2.6 2.6 2.8 2.8
-2.0 0.0 2.8 2.1 2.6 2.6 2.5 2.9 2.6 2.0 2.9 2.2 3.0 2.2 2.7
-3.0 -2.6 0.0 2.8 2.4 2.1 2.8 2.1 2.6 2.5 2.4 2.7 2.6 2.6 2.6
-2.5 -2.0 -3.0 0.0 2.6 2.4 2.9 2.2 2.5 2.1 2.5 2.0 2.3 2.7 2.2
-2.9 -2.8 -2.4 -2.1 0.0 2.5 2.8 2.8 2.8 2.0 2.8 2.2 2.8 2.4 2.4
-2.3 -2.7 -2.9 -2.7 -2.6 0.0 2.8 2.3 2.5 2.8 2.5 3.0 2.9 2.7 2.3
-2.2 -2.2 -2.6 -2.2 -2.9 -2.1 0.0 2.2 2.0 2.8 2.1 2.3 2.2 2.4 2.2
-2.4 -2.7 -3.0 -2.4 -2.3 -2.3 -2.5 0.0 2.2 2.7 2.9 2.1 2.9 2.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