Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Fortran Dictionary

  1. Home
  2. Fortran Dictionary
  3. Array Basics (1D and Multi-dimensional)

Array Basics (1D and Multi-dimensional)

In FORTRAN, arrays are used to handle multiple values of the same type together. A 1D array is declared with dimension(size) or in the form integer :: varname(size), and indices start at 1 (not 0). Multidimensional arrays are specified by listing dimensions as dimension(rows, cols). The array constructor (/ ... /) is convenient for assigning values to all elements at once.

Syntax

! -----------------------------------------------
! Declaring a 1D array (three ways)
! -----------------------------------------------

integer :: scores(5)                        ! integer array of 5 elements
real, dimension(5)    :: heights            ! real type, 5 elements
character(len=20), dimension(5) :: names    ! character array, 5 elements

! -----------------------------------------------
! Bulk assignment with array constructor
! -----------------------------------------------

integer :: scores(5)
scores = (/ 87, 92, 78, 95, 83 /)          ! assign all elements at once with (/ ... /)

! -----------------------------------------------
! Element access (index starts at 1)
! -----------------------------------------------

print *, scores(1)   ! access the 1st element
scores(3) = 100      ! overwrite the 3rd element

! -----------------------------------------------
! 2D array declaration and access
! -----------------------------------------------

integer :: matrix(3, 4)   ! integer array of 3 rows x 4 columns

matrix(2, 3) = 42         ! overwrite element at row 2, column 3
print *, matrix(2, 3)     ! display element at row 2, column 3

! -----------------------------------------------
! Array intrinsic functions
! -----------------------------------------------

integer :: arr(5) = (/ 10, 20, 30, 40, 50 /)

print *, size(arr)          ! returns number of elements -> 5
print *, sum(arr)           ! returns sum of all elements -> 150
print *, maxval(arr)        ! returns maximum value -> 50
print *, minval(arr)        ! returns minimum value -> 10
print *, maxloc(arr)        ! returns index of maximum value -> 5

Syntax Reference

SyntaxDescription
integer :: arr(N)Declares a 1D integer array with N elements. Index starts at 1.
real, dimension(N) :: arrDeclares a real array using the dimension attribute.
arr = (/ v1, v2, ... /)Assigns all elements at once using an array constructor.
arr(i)Accesses the i-th element of array arr (1-based).
integer :: mat(M, N)Declares a 2D integer array with M rows and N columns.
mat(i, j)Accesses the element at row i, column j of 2D array mat.
size(arr)Returns the number of elements in the array.
sum(arr)Returns the sum of all elements in the array.
maxval(arr)Returns the maximum value in the array.
minval(arr)Returns the minimum value in the array.
maxloc(arr)Returns the index of the element with the maximum value.
minloc(arr)Returns the index of the element with the minimum value.

Sample Code

jjk_array_1d.f90
! jjk_array_1d.f90 — Sample demonstrating declaration, assignment, and access for 1D arrays
! Manages cursed energy data of Jujutsu Kaisen sorcerers in a 1D array
!
! Compile and run:
!   gfortran jjk_array_1d.f90 -o jjk_array_1d && ./jjk_array_1d

program jjk_array_1d

    implicit none

    integer, parameter :: NUM_SORCERERS = 5   ! number of sorcerers (constant)

    ! -----------------------------------------------
    ! 1D array declarations
    ! -----------------------------------------------
    character(len=20), dimension(NUM_SORCERERS) :: sorcerer_names   ! sorcerer names
    integer,           dimension(NUM_SORCERERS) :: cursed_energy    ! cursed energy value
    real,              dimension(NUM_SORCERERS) :: technique_power  ! technique power (multiplier)

    integer :: i          ! loop counter
    integer :: total      ! total cursed energy
    integer :: max_idx    ! index of maximum cursed energy

    ! -----------------------------------------------
    ! Bulk assignment with array constructor
    ! -----------------------------------------------
    sorcerer_names(1) = "Itadori Yuji"
    sorcerer_names(2) = "Fushiguro Megumi"
    sorcerer_names(3) = "Kugisaki Nobara"
    sorcerer_names(4) = "Gojo Satoru"
    sorcerer_names(5) = "Nanami Kento"

    ! bulk assign cursed energy values with array constructor
    cursed_energy  = (/ 8500, 7200, 6800, 99999, 7800 /)

    ! bulk assign technique power with array constructor
    technique_power = (/ 2.5, 3.1, 2.8, 9.9, 3.0 /)

    ! -----------------------------------------------
    ! Display all sorcerer data using a DO loop
    ! -----------------------------------------------
    print *, "===== Sorcerer Cursed Energy Data ====="
    do i = 1, NUM_SORCERERS
        print *, trim(sorcerer_names(i)), " | Energy:", cursed_energy(i), &
                 " | Technique Power:", technique_power(i)
    end do
    print *, "======================================="

    ! -----------------------------------------------
    ! Aggregate with intrinsic functions
    ! -----------------------------------------------
    total   = sum(cursed_energy)               ! total cursed energy of all sorcerers
    max_idx = maxloc(cursed_energy, dim=1)     ! get index of maximum cursed energy

    print *, "Total Energy  :", total
    print *, "Strongest     :", trim(sorcerer_names(max_idx)), &
             " (Energy:", cursed_energy(max_idx), ")"

    ! -----------------------------------------------
    ! Overwrite a specific element by index
    ! -----------------------------------------------
    print *, "--- Gojo Satoru's energy updated after Domain Expansion ---"
    cursed_energy(4) = 150000   ! update energy at index 4 (Gojo Satoru)
    print *, trim(sorcerer_names(4)), " updated energy:", cursed_energy(4)

end program jjk_array_1d
gfortran jjk_array_1d.f90 -o jjk_array_1d && ./jjk_array_1d
===== Sorcerer Cursed Energy Data =====
Itadori Yuji     | Energy:        8500  | Technique Power:   2.50000000
Fushiguro Megumi | Energy:        7200  | Technique Power:   3.09999990
Kugisaki Nobara  | Energy:        6800  | Technique Power:   2.79999995
Gojo Satoru      | Energy:       99999  | Technique Power:   9.90000057
Nanami Kento     | Energy:        7800  | Technique Power:   3.00000000
=======================================
Total Energy  :      130299
Strongest     : Gojo Satoru  (Energy:       99999 )
--- Gojo Satoru's energy updated after Domain Expansion ---
Gojo Satoru  updated energy:      150000
jjk_array_2d.f90
! jjk_array_2d.f90 — Sample demonstrating declaration, assignment, and access for 2D arrays
! Uses Jujutsu Kaisen characters to verify operations on a
! 2D array (sorcerers x attribute scores)
!
! Compile and run:
!   gfortran jjk_array_2d.f90 -o jjk_array_2d && ./jjk_array_2d

program jjk_array_2d

    implicit none

    integer, parameter :: NUM_SORCERERS = 5   ! number of sorcerers
    integer, parameter :: NUM_STATS     = 4   ! number of stats (ATK, DEF, SPD, Energy)

    ! -----------------------------------------------
    ! 2D array declaration (NUM_SORCERERS rows x NUM_STATS columns)
    ! -----------------------------------------------
    character(len=20) :: sorcerer_names(NUM_SORCERERS)   ! sorcerer names
    character(len=10) :: stat_labels(NUM_STATS)           ! stat labels
    integer           :: stats(NUM_SORCERERS, NUM_STATS)  ! stats 2D array

    integer :: i, j       ! loop counters
    integer :: row_sum    ! row total (total score for one person)
    integer :: col_max    ! column maximum (highest value per stat)
    integer :: best_idx   ! index of the sorcerer with the highest value

    ! -----------------------------------------------
    ! Set sorcerer names and stat labels
    ! -----------------------------------------------
    sorcerer_names(1) = "Itadori Yuji"
    sorcerer_names(2) = "Fushiguro Megumi"
    sorcerer_names(3) = "Kugisaki Nobara"
    sorcerer_names(4) = "Gojo Satoru"
    sorcerer_names(5) = "Nanami Kento"

    stat_labels(1) = "ATK"
    stat_labels(2) = "DEF"
    stat_labels(3) = "SPD"
    stat_labels(4) = "Energy"

    ! -----------------------------------------------
    ! Assign each sorcerer's stats to the 2D array
    ! Access as stats(sorcerer_index, stat_index)
    ! -----------------------------------------------
    ! Itadori Yuji    : ATK 95 / DEF 70 / SPD 85 / Energy 80
    stats(1, 1) = 95;  stats(1, 2) = 70;  stats(1, 3) = 85;  stats(1, 4) = 80

    ! Fushiguro Megumi: ATK 80 / DEF 75 / SPD 78 / Energy 88
    stats(2, 1) = 80;  stats(2, 2) = 75;  stats(2, 3) = 78;  stats(2, 4) = 88

    ! Kugisaki Nobara : ATK 82 / DEF 65 / SPD 72 / Energy 78
    stats(3, 1) = 82;  stats(3, 2) = 65;  stats(3, 3) = 72;  stats(3, 4) = 78

    ! Gojo Satoru     : ATK 99 / DEF 99 / SPD 99 / Energy 99
    stats(4, 1) = 99;  stats(4, 2) = 99;  stats(4, 3) = 99;  stats(4, 4) = 99

    ! Nanami Kento    : ATK 85 / DEF 80 / SPD 76 / Energy 84
    stats(5, 1) = 85;  stats(5, 2) = 80;  stats(5, 3) = 76;  stats(5, 4) = 84

    ! -----------------------------------------------
    ! Display all sorcerer stats
    ! -----------------------------------------------
    print *, "===== Sorcerer Stats ====="
    print *, "Name             | ATK  | DEF  | SPD  | Enrg | Total"
    print *, "-------------------------------------------------"

    do i = 1, NUM_SORCERERS
        row_sum = 0
        do j = 1, NUM_STATS
            row_sum = row_sum + stats(i, j)   ! sum all stats for one sorcerer
        end do
        print "(A16, ' | ', 4(I4, ' | '), I5)", &
              trim(sorcerer_names(i)), &
              stats(i, 1), stats(i, 2), stats(i, 3), stats(i, 4), row_sum
    end do

    ! -----------------------------------------------
    ! Find the sorcerer with the highest value per stat
    ! -----------------------------------------------
    print *, ""
    print *, "===== Best per Stat ====="
    do j = 1, NUM_STATS
        col_max  = stats(1, j)
        best_idx = 1
        do i = 2, NUM_SORCERERS
            if (stats(i, j) > col_max) then
                col_max  = stats(i, j)
                best_idx = i
            end if
        end do
        print *, trim(stat_labels(j)), "best :", col_max, &
                 " (", trim(sorcerer_names(best_idx)), ")"
    end do
    print *, "========================="

end program jjk_array_2d
gfortran jjk_array_2d.f90 -o jjk_array_2d && ./jjk_array_2d
===== Sorcerer Stats =====
Name             | ATK  | DEF  | SPD  | Enrg | Total
-------------------------------------------------
Itadori Yuji     |   95 |   70 |   85 |   80 |   330
Fushiguro Megumi |   80 |   75 |   78 |   88 |   321
Kugisaki Nobara  |   82 |   65 |   72 |   78 |   297
Gojo Satoru      |   99 |   99 |   99 |   99 |   396
Nanami Kento     |   85 |   80 |   76 |   84 |   325

===== Best per Stat =====
ATK best :   99  ( Gojo Satoru )
DEF best :   99  ( Gojo Satoru )
SPD best :   99  ( Gojo Satoru )
Energy best :   99  ( Gojo Satoru )
=========================

Common Mistakes

Confusing 0-based and 1-based indexing

Fortran array indices start at 1 by default. Confusing this with C's 0-based indexing and writing index 0 causes an out-of-bounds runtime error. By default, arr(0) does not exist.

NG (writing 0-based index)
integer :: arr(5)

arr(0) = 100   ! runtime error: out of bounds (Fortran is 1-based)
arr(1) = 200
OK (writing 1-based index)
integer :: arr(5)

arr(1) = 100   ! arr(1) through arr(5) are valid
arr(2) = 200
arr(5) = 500

Accessing beyond array bounds (out-of-bounds access)

Accessing an index beyond the declared size does not cause a compile error but causes a runtime error (detectable with the gfortran -fbounds-check option). Pay attention to loop upper bounds.

integer :: arr(5)
integer :: i

arr = (/ 10, 20, 30, 40, 50 /)

! NG: loop upper bound exceeds array size
do i = 1, 6   ! when i=6, accesses arr(6) -> out of bounds
    print *, arr(i)
end do

! OK: use size() for the loop upper bound
do i = 1, size(arr)   ! size(arr) = 5, so safe
    print *, arr(i)
end do

Array constructor element count does not match the declaration

If the number of elements in the array constructor (/ v1, v2, ... /) does not match the declared size, it causes a compile error. Make sure element counts match exactly when assigning.

NG (element count mismatch)
integer :: arr(5)

arr = (/ 10, 20, 30 /)   ! compile error: 3 elements (declaration is 5)
OK (element counts match)
integer :: arr(5)

arr = (/ 10, 20, 30, 40, 50 /)   ! 5 elements match

Overview

A key characteristic of FORTRAN arrays that differs from C and similar languages is that indices start at 1. A 1D array can be declared as integer :: arr(N) or integer, dimension(N) :: arr, and all elements can be assigned at once using the array constructor (/ v1, v2, ... /). A 2D array is declared as integer :: mat(M, N) and accessed with mat(i, j) specifying row and column. Using the intrinsic functions sum, maxval, minval, maxloc, and size lets you write aggregation logic simply without DO loops. For loops used together with arrays, see DO Loop. For element types in arrays, also see INTEGER / REAL.

If you find any errors or copyright issues, please .