next up previous contents
Next: About this document ... Up: Overview of FORTAN 77 Previous: Control Structures

Subsections

Arrays

Arrays are collections of data of the same type. They are the most important data type in scientific computing where we usually deal with a great number of, let's say, data points.

Declaration of Arrays

Arrays are declared in the same way as ordinary variables, only by specifiying the starting and ending indices (subscripts):
type array({Lower bound in $n^{\text{th}}$ dimension:Upper bound in $n^{\text{th}}$ dimension})

For example, to declare an array used to store your income for several years and all the months in each year, use either of these:


       REAL income(12,1995:1999)
       REAL income(1:12,1995:1999)

The lower bounds are optional and default to one. The arrays can have up to 7 dimensions. The size of the array must be determined at compilation time (in most cases). They are stored in contingent blocks of memory and are column-ordered (this is important when exploring an array with nested DO-loops--put the column loop inside the row loop to increase speed).

Using Arrays

Arrays are usually accessed on an element-by-element basis (at least in this old version of Fortran). To access a specific element of the array, simply enclose the list of subscripts for that element in parenthesis:
array({List of subscripts})
For example, to find your july income in 1996, use:


       july_income=income(6,1996)

In certain occassions arrays can be used as a whole, without subscripts. The most important are when printing or reading an array or passing the array to a function as an argument. The first case is trivial. For example, to print a whole array, just use:


       WRITE(*,*) array

The second case is quite tricky in Fortran 77.

Arrays as Arguments to Functions

Passing arrays to functions is not one of the highlights of Fortran 77, and many improvements exist in Fortran 90. The main problem is that the arrays contain no information about their own size, so that the size of the arrays passed as arguments to functions have to be either fixed-size arrays, or the size of the arrays has to actually be passed to the function as an extra argument--adjustible arrays. If the size of the array is unknown, than it does not have to be specified, and we can use assumed-size arrays. We declare this with an * as the size of the last dimension (other dimensions can not be assumed). But remeber that the compiler has no information about the size of the array and so we must ensure that we stay within the bounds of the array. Also, this excludes using the arrays in READ and WRITE statements without subscripts.

For example, to define a function that calculates the norm of a vector (a one dimensional array), we would type:


       REAL FUNCTION Norm(array,array_start,array_end)
         REAL sum=0.0,array(array_start,array_end)
         INTEGER i
         DO 10, i=array_start,array_end
           sum=sum+array(i)**2
10       CONTINUE
         Norm=SQRT(sum)
       END Norm


next up previous contents
Next: About this document ... Up: Overview of FORTAN 77 Previous: Control Structures
Aleksandar Donev
2000-01-13