Arrays in Fortran
Arrays are central to Fortran because the language was designed for numerical and scientific computing, where data naturally comes in vectors, matrices, and higher-dimensional grids. Fortran arrays can have up to 7 dimensions, support flexible lower bounds (not just starting at 1), and modern Fortran (90+) adds whole-array arithmetic, array sections, and dynamic allocation via ALLOCATABLE that eliminate much of the manual indexing loops that older FORTRAN 77 code required.
Cricket analogy: A season's batting scorecard stored as runs(1:11) for eleven players is a natural Fortran array — one contiguous block of numbers indexed by batting position, just as a scorer tracks Virat Kohli's runs at index 3 without needing separate variables for every player.
Declaring and Indexing Arrays
Arrays are declared with a type and a dimension attribute, e.g., REAL, DIMENSION(10) :: x or the equivalent REAL :: x(10), and by default the valid index range is 1 to 10; an explicit lower bound can be set with x(0:9) or even negative bounds like x(-5:5). Attempting to access an index outside the declared bounds is undefined behavior that many compilers won't catch unless bounds-checking is explicitly enabled (e.g., gfortran -fbounds-check), making it a common source of silent memory corruption bugs.
Cricket analogy: A T20 innings array overs(1:20) naturally starts numbering at 1, but a Test match's day-by-day scoring could use day(0:4) to include a symbolic 'pre-match' index 0, just as Fortran lets you redefine an array's lower bound instead of always starting at 1.
PROGRAM matrix_demo
IMPLICIT NONE
INTEGER, PARAMETER :: rows = 3, cols = 3
REAL :: A(rows, cols), col_sum(cols)
INTEGER :: i, j
DO j = 1, cols
DO i = 1, rows
A(i, j) = REAL(i * 10 + j)
END DO
END DO
DO j = 1, cols
col_sum(j) = SUM(A(:, j))
END DO
PRINT *, 'Column sums: ', col_sum
PRINT *, 'Max element: ', MAXVAL(A)
PRINT *, 'Middle row section: ', A(2, :)
END PROGRAM matrix_demoArray Sections and Whole-Array Operations
Modern Fortran supports array sections using the colon syntax, e.g., x(2:5) selects elements 2 through 5, and x(:) or simply x refers to the whole array; whole-array arithmetic like y = x * 2.0 or z = x + y applies the operation element-wise without an explicit DO loop, and intrinsic functions like SUM(x), MAXVAL(x), MINLOC(x), and DOT_PRODUCT(a, b) operate on entire arrays or sections directly, often compiling to vectorized machine code faster than a hand-written loop.
Cricket analogy: Selecting overs(16:20) from a 20-over innings array to analyze just the death overs is an array section, and computing SUM(runs) for the whole innings array gives the total score without writing a manual accumulation loop.
Whole-array intrinsics like SUM, PRODUCT, MAXVAL, MINVAL, and COUNT accept an optional DIM argument, e.g., SUM(A, DIM=1) sums down each column, returning a rank-1 result — a common pattern for reducing one dimension of a matrix without writing an explicit loop.
Multidimensional Arrays and Storage Order
Fortran arrays are stored in column-major order, meaning for a 2D array A(rows, cols), consecutive elements down the first dimension (down a column) are contiguous in memory before moving to the next column — the opposite of C's row-major order. This matters enormously for performance: looping with the leftmost index varying fastest in the innermost loop (DO j = 1, cols; DO i = 1, rows; ... A(i,j) ...) accesses memory sequentially and is dramatically faster than looping in the wrong order, which causes cache misses on large arrays.
Cricket analogy: A scoresheet grid runs(batter, over) stored column-major means all of one over's data across every batter sits together in memory before the next over's column begins — like flipping through a printed scorecard over-by-over rather than batter-by-batter for fastest reading.
Looping over a 2D array with the wrong index order — e.g., DO i = 1, rows; DO j = 1, cols; ...A(i,j)... — walks across a row on the inner loop, which strides through memory non-contiguously in Fortran's column-major layout and can be several times slower on large arrays due to cache misses.
- Fortran arrays default to a 1-based lower bound but can be redeclared with any lower bound, including 0 or negative values.
- Out-of-bounds array access is not caught by default; compile with -fbounds-check (gfortran) during development to catch it.
- Array sections use colon syntax, e.g. x(2:5), and whole-array operations like y = x * 2.0 apply element-wise without a manual loop.
- Intrinsics like SUM, MAXVAL, MINLOC, and DOT_PRODUCT operate directly on whole arrays or sections.
- Fortran stores multidimensional arrays in column-major order — the leftmost index varies fastest in memory.
- For best performance, the innermost loop should vary the leftmost array index to access memory sequentially.
- Arrays can have up to 7 dimensions and can be fixed-size, ALLOCATABLE, or passed as assumed-shape dummy arguments.
Practice what you learned
1. What is the default lower bound of a Fortran array declared as REAL :: x(10)?
2. Which memory layout does Fortran use for multidimensional arrays?
3. What does the intrinsic function SUM(A, DIM=1) do for a 2D array A?
4. Why is it important to enable bounds-checking (e.g., -fbounds-check) during Fortran development?
5. What does the array section A(2, :) refer to for a 2D array A?
Was this page helpful?
You May Also Like
Loops in Fortran
Fortran's DO, DO WHILE, and indefinite loop constructs, plus EXIT and CYCLE control flow.
Subroutines and Functions
Structuring reusable Fortran code with SUBROUTINEs, FUNCTIONs, INTENT, and pass-by-reference arguments.
Modules in Fortran
Packaging types, data, and procedures into reusable, compile-time-checked Fortran modules.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics