next up previous contents
Next: Application Up: A quick tour of Previous: Application   Contents


Arrays are collection of elements of similar type. This data structure allows to implement mathematical objects like vectors, matrices and tensors.

To declare an array with 3 real elements we use

  real, dimension(3) :: v  or
  real :: v(3)
  real v(3)
Particular entries of the array can be accessed with an index, running from 1 through the number of entries.
v(1)=1.0; v(2)=2.0; v(3)=3.0
One can assign values to all elements of v at once using an array constructor. A constructor is a list of scalar values delimited by (/ ... /). For example the same initialization of v can be achieved using
v = (/ 1.0, 2.0, 3.0 /)
An even more compact initialization is achieved using an implicit do loop:
v = (/ (i, i=1,3) /)

In Fortran jargon the one-dimensional object v is called a rank-1 array (its entries are accessed using a single index). One can define multi-dimensional arrays as well. A rank-2 array can be defined for example as

  real, dimension(2,3) :: A  or
  real :: A(2,3)
  real A(2,3))
A particular element is now accessed using two indices: A(i,j) where and .

One can initialize a rank-n array using constructors. The list of entries in a constructor is one-dimensional; we need to map this one-dimensional list onto the k-dimensional array. This is done using the intrinsic function reshape. For example, we can initialize the 6 entries of A using

A = reshape( (/ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 /), (/ 2, 3 /) )
The first argument of the reshape function is the constructor, the second is another constructor containing the shape of A. Reshape initializes the entries of A in column-wise order; the result of the above operation is

Arrays can be used as arguments for input/output purposes. For example

read*, A
will (expect to) read 6 numbers from the console and assign the values to elements of A in column-wise order. Similarly,
print*, A
prints the elements of A one after another in column-wise order.

next up previous contents
Next: Application Up: A quick tour of Previous: Application   Contents
Adrian Sandu 2001-08-26