Skip to main content

Two dimensional arrays in FORTRAN

Short answer

A two-dimensional array is declared as type name(rows, columns) or DIMENSION name(rows, cols), e.g., INTEGER A(3,4) for 3 rows and 4 columns. Subscripts start from 1 (by default) and storage is column-major; an element is referenced as A(i,j).

What is the syntax of two dimensional arrays in FORTRAN? Explain with example.

A two dimensional array is a table of rows and columns. It must be declared before use, either with a type statement or with DIMENSION.

Syntax

      type name(rows, columns)
      DIMENSION name(rows, columns)

Examples: INTEGER A(3,4) (3 rows, 4 columns, 12 elements) and REAL B(2,2).

  • Subscripts start from 1 by default: A(1,1) is the first element. Other bounds can be given as A(0:2, 0:3).
  • An element is written A(i,j), row i and column j.
  • FORTRAN stores arrays in column-major order (the first subscript changes fastest).
  • FORTRAN 77 allows up to seven dimensions.
  • The subscripts can be integer expressions, and nested DO loops or nested implied DO are used to process them.

Example: read and print a 2×3 array

      PROGRAM ARR2D
      INTEGER A(2,3), I, J
      READ *, ((A(I,J), J = 1, 3), I = 1, 2)
      DO 10 I = 1, 2
         PRINT *, (A(I,J), J = 1, 3)
   10 CONTINUE
      STOP
      END

For input 1 2 3 4 5 6 it prints 1 2 3 and then 4 5 6.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 11 questions →