« previous next»

5.1    One Dimensional Arrays

    One of the most important features of Fortran 90 is that you can declare data as arrays.  Fortran 90 also allows manipulation and operation of intrinsic functions on these arrays (discussed in section 5.3).  First we will consider one dimensional arrays.

    Data is declared as an array in the following manner:

    type, DIMENSION(min:max) :: array names

    Type is the data type for all the items in the array.  Arrays can be any of the data types described in 2.1.  In section 5.4 the possibility of derived data types (where different items in an array are different types) is discussed.  Min and max are the minimum and maximum subscripts used by the arrays.  If the minimum subscript is one then min can be excluded. Array names is a list of arrays that will have this type with the specified DIMENSION.  The names are separated by a comma.

    Examples:

    REAL, DIMENSION(1:20) :: A, B
    REAL, DIMENSION(20) :: A, B
    INTEGER, DIMENSION(-1:3) :: C

    All items in the arrays A and B are of type REAL.  Space will be allocated for these array during compilation.  A and B has space for 20 REAL numbers.  C has space for 5 INTEGER numbers, subscripted -1, 0, 1, 2, and 3.  Because arrays can require large amounts of memory, sometimes it is useful to be able to allocate (and deallocate) space in memory for the array during execution.  This will be discussed in 5.2.

    For now, we will consider one dimensional, compile-time allocated arrays with items all of the same type.
 

POPULATING ARRAYS

    Populating arrays (i.e. assigning values to their elements) can be performed in many ways.  An easy and compact way is by using explicit DO loops with the subscript changing on each pass through the loop.

    REAL, DIMENSION(5) :: A

    DO I = 1, 5
        READ (10, *) A(I)
    END DO

In this case the data comes from UNIT = 10 (which we will assume is a data file).  The data must be on separate lines because each READ statement will start on a separate line.

    Arrays can also be read be using the array name without subscripts.  Array A could have been read from UNIT 10:

    READ (10, *) A

Now the READ statement is used only once so the data can be on one line (separated by spaces or commas), on five separate lines, three numbers on one line and then two on the next, or whatever you desire.

    Finally, arrays can be read using an implied DO loop:

    READ (10, *) (A(I), I = 1, 5)

Again, the READ statement is encountered only once the date does not have to be on separate lines.
 

Practice: Create a text file and type the following data:

1.0 7.2 5.9 -7.8 2.7
4.9 -3.1 7.8 2.9 1.2
3.9 4.8 9.1 10.3 0.0

Save the file and write a program to read the first line. Then write a program that reads the first column.

ACCESSING INDIVIDUAL ELEMENTS

    As implied above, accessing single elements of an array is performed using the array name followed by the subscript contained within parentheses ().  You can set add two elements of an array:

    Sum = A(3) + A(5)

here the value for Sum is the third element of array A plus the fifth element.   A sum of all the elements in an array could be accessed using a DO loop:

    Sum = 0
    DO I = 1,5
        Sum = Sum + A(I)
    END DO
 

DIRECT ACCESS

    One advantage of arrays over multiple single variables is direct access to items in an array.  For example if you want to read 20 values and store them in the memory, you could store them as 20 separate values:

    REAL :: input_1, input_2, ... input_20
    READ (10, *) input_1, input_2, ... input_20   !  .. are not legal Fortran commands, just an indication of repetition

Or you could create an array:

    REAL, DIMENSION (20)  :: input
    READ (10, *) (input (I), I = 1, 20)

In the first case, if you want to access item 20 then it is accessed in the memory sequentially, starting from the beginning.  The first 19 must be looked at first before input_20 is encountered.  In the case of the array,  Input(20) is direct access.  The 20 specifies its location and the first 19 are skipped.  From this example you can see the space and time saved by using arrays.
 

ARRAY ASSIGNMENT

    Arrays can be assigned values within your program by containing them within (/ and /).  Only values can be contained within these symbols or implied DO loops can be used.  For example:

    INTEGER, DIMENSION (5) :: A
    A = (/ 2, 4, 6, 8, 10/)
    A = (/  (2*I, I = 1,5) /)
    A = (/ 2, (2*I, I = 2, 4), 10 /)

All of these A declarations created the same array A.  Arrays can also be assigned using a simple value:

    A = 0

this assigns the value of 0 to all elements of A.
 

SUBSETS

    Arrays can be broken down into smaller components, or subsets.  This is done as follows:

    array_name(min:max: step)

Where min is the minimum subscript of the array, max is the maximum subscript considered, and step is the step size for the subscripts indicating the subscripts contained within your subarray.  For example:

    REAL, DIMENSION (5) :: A
    REAL, DIMENSION (3) :: B, C
    A = (/ 10, 20, 30, 40, 50/)
    B = A(1:5:2)
    C = A(2:4)

then B contains the first, third, and fifth elements of A (i.e. B = (/ 10, 30, 50/)).  C contains the middle three elements of A (i.e. C = (/ 20, 30, 40 /)).
 

INTRINSIC FUNCTIONS

    Fortran 90 allows various intrinsic functions to be used on arrays.  Some of the more common ones are:

    DOT_PRODUCT(A, B)           Returns the dot product of A and B
    MAXVAL(A)                           Returns the maximum value in array A
    MINVAL(A)                            Returns the minimum value in array A
    PRODUCT(A)                         Returns the product of all the elements in A
    SIZE(A)                                   Returns the number of elements in A
    SUM(A)                                  Returns the sum of all the elements in A

« previous next»