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