next up previous contents index
Next: Fortran in Practice Up: Basic Fortran Concepts Previous: IF-blocks

Arrays

Fortran has good facilities for handling arrays. They can have up to seven dimensions. The program STATS reads a set of real numbers from a data file and puts them into a one-dimensional array. It then computes their mean and standard deviation. Given an array of values x1, x2, x3, ... xN, the mean $\mu$ standard deviation $\sigma$ are given by:

\begin{displaymath}
\mu = \frac{\sum x_{i}}{N} \end{displaymath}

\begin{displaymath}
\sigma^{2} = \frac{(\sum (x_{i} - \sigma)^{2})}{(N-1)} = 
\frac{(\sum x_{i}^{2} - N \sigma)}{( N - 1 )} \end{displaymath}

To simplify this program, it will be assumed that the first number in the file is an integer which tells the program how many real data points follow.

 
       PROGRAM STATS 
       CHARACTER FNAME*50 
       REAL X(1000) 
       WRITE(UNIT=*, FMT=*) 'Enter data file name:' 
       READ(UNIT=*, FMT='(A)') FNAME 
       OPEN(UNIT=1, FILE=FNAME, STATUS='OLD') 
*Read number of data points NPTS 
       READ(UNIT=1, FMT=*) NPTS 
       WRITE(UNIT=*, FMT=*) NPTS, ' data points' 
       IF(NPTS .GT. 1000) STOP 'Too many data points' 
       READ(UNIT=1, FMT=*) (X(I), I = 1,NPTS) 
       CALL MEANSD(X, NPTS, AVG, SD) 
       WRITE(UNIT=*, FMT=*) 'Mean =', AVG, ' Std Deviation =', SD 
       END 

       SUBROUTINE MEANSD(X, NPTS, AVG, SD) 
       INTEGER NPTS 
       REAL X(NPTS), AVG, SD 
       SUM   = 0.0 
       SUMSQ = 0.0 
       DO 15, I = 1,NPTS 
            SUM   = SUM   + X(I) 
            SUMSQ = SUMSQ + X(I)**2 
15       CONTINUE 
       AVG = SUM / NPTS 
       SD  = SQRT(SUMSQ - NPTS * AVG)/(NPTS-1) 
       END
This program has several new statement forms.

The CHARACTER statement declares that the variable FNAME is to hold a string of 50 characters: this should be long enough for the file-names used by most operating systems.

The REAL statement declares an array X with 1000 elements numbered from X(1) to X(1000).

The READ statement uses a format item A which is needed to read in a character string: A originally stood for ``alpha-numeric".

The OPEN statement then assigns I/O unit number one (any small integer could have been used) to the file. This unit number is needed in subsequent input/output statements. The item STATUS='OLD' is used to specify that the file already exists.

The IF statement is a special form which can replace an IF-block where it would only contain one statement: its effect is to stop the program running if the array would not be large enough.

The READ statement which follows it has a special form known as an implied-DO-loop: this reads all the numbers from the file in to successive elements of the array X in one operation.

The CALL statement corresponds to the SUBROUTINE statement in the same way that a function reference corresponded to a FUNCTION statement. The difference is that the arguments X and NPTS transfer information into the subroutine, whereas AVG and SD return information from it. The direction of transfer is determined only by the way the dummy arguments are used within the subroutine. An argument can be used to pass information in either direction, or both.

The INTEGER statement is, as before, not really essential but it is good practice to indicate clearly the data type of every procedure argument.

The REAL statement declares that X is an array but uses a special option available only to dummy arguments: it uses another argument, NPTS, to specify its size and makes it an adjustable array. Normally in Fortran array bounds must be specified by constants, but the rules are relaxed for arrays passed into procedures because the actual storage space is already allocated in the calling program unit; the REAL statement here merely specifies how many of the 1000 elements already allocated are actually to be used within the subroutine.

The rest of the subroutine uses a loop to accumulate the sum of the elements in SUM, and the sum of their squares in SUMSQ. It then computes the mean and standard deviation using the usual formulae, and returns these values to the main program, where they are printed out.


next up previous contents index
Next: Fortran in Practice Up: Basic Fortran Concepts Previous: IF-blocks
Helen Rowlands
8/27/1998