Arrays

Contents
Declare shape
1-based indexing
Initialize
Whole-array ops

Declare shape

An array declaration states the element type and the size in each dimension inside parentheses.

A one dimensional array is a list, while a two dimensional array is a table with rows and columns.

program shapedemo implicit none real :: v(5) real :: grid(3, 4) print *, size(v) print *, size(grid) end program shapedemo

1-based indexing

By default the first element is number 1, the second is number 2, and so on up to the declared size.

A loop from 1 to size walks the whole array safely without touching memory outside it.

program indexdemo implicit none integer :: v(3) integer :: i v(1) = 10 v(2) = 20 v(3) = 30 do i = 1, 3 print *, i, v(i) end do end program indexdemo

Initialize

Arrays can start with one shared value or with an explicit list in square brackets.

The bracket form lists elements in index order and must hold exactly as many values as the array size.

program initdemo implicit none real :: a(3) = 0.0 integer :: b(3) = [1, 2, 3] print *, a print *, b end program initdemo

Whole-array ops

Fortran applies arithmetic to whole arrays at once, with no loop needed for simple element by element math.

Intrinsics such as sum then reduce the array to one answer in a single call.

program wholedemo implicit none real :: a(3) = [1.0, 2.0, 3.0] real :: b(3) b = a * 2.0 + 1.0 print *, b print *, sum(b) end program wholedemo

Next: Array sections

Article author: Arthur Isaev

Related articles
Fortran
Fortran course: zero to hero in 50 lessons
What is Fortran and where it runs
Install gfortran
Your first program
Program structure
Types: integer real complex logical character
Kind parameters
Operators
Conditions
DO loops
Arrays
Array sections
Character strings
Formatted IO
Files
Subroutines and functions
Modules
USE with ONLY
INTENT
OPTIONAL and PRESENT
Explicit interfaces
Derived types
Type-bound procedures
Object orientation
Operator overloading
Finalization
Allocatable arrays
Pointers in Fortran
DO CONCURRENT
Coarrays
OpenMP basics
Namelists
IEEE arithmetic
Intrinsic matrix math
Random numbers
String tools
Command-line arguments
Environment variables
error stop
Testing
Build with gfortran and make
Debugging
C interoperability
From fixed form to free form
Numeric models
Performance
Modern Fortran tour: 2008, 2018, 2023
Capstone: stats tool
Capstone: 1D heat equation
Style guide
Hero roadmap: the whole course on one page
Install gfortran on Windows 11
Install gfortran on Ubuntu
Install gfortran on Rocky Linux
Install gfortran on macOS
Install gfortran on FreeBSD
Fortran in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: