Title here
Summary here
In Co-array Fortran, an array is a numbered sequence of elements of a specific length. Arrays are fundamental in Fortran and are used extensively in scientific computing and numerical analysis.
program arrays
implicit none
integer :: i, j
integer :: a(5)
integer :: b(5)
integer :: twoD(2,3)
! Here we create an array 'a' that will hold exactly 5 integers.
! By default, an array is zero-valued in Fortran.
print *, "emp:", a
! We can set a value at an index using the array(index) = value syntax,
! and get a value with array(index).
a(5) = 100
print *, "set:", a
print *, "get:", a(5)
! The intrinsic function 'size' returns the length of an array.
print *, "len:", size(a)
! Use this syntax to declare and initialize an array in one line.
b = [1, 2, 3, 4, 5]
print *, "dcl:", b
! In Fortran, you can't have the compiler count the number of elements for you.
! You need to explicitly specify the size or use dynamic allocation.
! If you want to initialize only specific elements, you can use this syntax:
b = [100, 0, 0, 400, 500]
print *, "idx:", b
! Array types are multi-dimensional by default in Fortran.
! Here's how to create and initialize a 2D array:
do i = 1, 2
do j = 1, 3
twoD(i,j) = i + j - 2
end do
end do
print *, "2d: ", twoD
! You can create and initialize multi-dimensional arrays at once too.
twoD = reshape([1, 2, 3, 1, 2, 3], [2, 3])
print *, "2d: ", twoD
end program arrays
Note that arrays in Fortran are printed in column-major order when using print *
.
To compile and run this Fortran program:
$ gfortran -coarray=single arrays.f90 -o arrays
$ ./arrays
emp: 0 0 0 0 0
set: 0 0 0 0 100
get: 100
len: 5
dcl: 1 2 3 4 5
idx: 100 0 0 400 500
2d: 0 1 2 1 2 3
2d: 1 2 3 1 2 3
In Co-array Fortran, arrays are a fundamental feature and are used extensively in scientific computing and numerical analysis. The language provides powerful array operations and intrinsic functions for array manipulation.