Reading — step 1 of 5
Learn
~1 min readArrays and Loops
Fortran's identity is array math. Operations apply element-wise without explicit loops — the compiler vectorizes for you, often beating hand-written C.
program demo
implicit none
integer, dimension(5) :: a, b, c
integer :: i
a = [1, 2, 3, 4, 5] ! array literal
b = [(i*10, i = 1, 5)] ! 10, 20, 30, 40, 50
c = a + b ! element-wise
print *, c ! 11 22 33 44 55
print *, sum(c) ! 165
print *, maxval(c) ! 55
end program demo
Fortran arrays are 1-indexed by default. You can declare any range:
integer, dimension(0:9) :: zero_indexed ! 0 to 9
integer, dimension(-5:5) :: shifted ! -5 to 5
Built-in array intrinsics are extensive: sum, product, maxval, minval, count, any, all, dot_product, matmul, reshape, pack, unpack.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…