Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Fortran 90 Input/Output and Arrays: A Comprehensive Guide, Study notes of Research Methodology

An introduction to fortran 90's list-directed input and output, focusing on simple input and output using the read and write statements. It also covers arrays, including their declaration, subscripting, and assigning values. Examples and rules for input and output records, as well as a discussion on reading data from external files.

Typology: Study notes

2012/2013

Uploaded on 10/03/2013

abani
abani 🇮🇳

4.4

(33)

83 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Fortran 90 Input/Output and Arrays
1. Simple Input and Output (Compiled primarily from Hahn [1994])
The process of getting information into and out of the computer is an aspect of what is called data
transfer. The simplest form of data transfer in Fortran is with READ and WRITE and is called list-directed.
More advanced forms of data transfer will be covered later and can be found in any standard Fortran 90
references.
1.1. Input
You have learned that variables can be given values by using numeric assignment statements, as in the
following example to assign values to variables of BALANCE and RATE (interest rate):
BALANCE = 1000
RATE = 0.09
This is an inflexible way of supplying data, since to run the program for different balances or interest
rates you would have to find and change these statements. There may be many such assignments in a more
complicated program, and it is a waste of time to recompile them every time you want to change the data.
The READ* statement, however, allows you to supply the data while the program is running. Replace these
two assignment statements with the single statement
READ (*, *) BALANCE, RATE
When you run the program, the compiler will wait for you to type the values of the two variables at the
keyboard, if you are using a PC (an IBM compatible personal computer). They may be on the same line,
separated by blanks or a comma, or on different lines. You can correct a number with the backspace key
while entering it.
The general form of the READ statement is
READ (*, *) list
where list is a list of variables separated by commas.
Note the following general rules:
A single line of input or output is called a record (e.g., in the case of a PC, from the keyboard or on the
screen).
Each READ statement requires a new input record, e.g., the statement
READ (*, *) A, B, C
will be satisfied with one record containing three values:
3 4 5
whereas the statements
pf3
pf4
pf5

Partial preview of the text

Download Fortran 90 Input/Output and Arrays: A Comprehensive Guide and more Study notes Research Methodology in PDF only on Docsity!

Fortran 90 Input/Output and Arrays

1. Simple Input and Output (Compiled primarily from Hahn [1994])

The process of getting information into and out of the computer is an aspect of what is called data transfer. The simplest form of data transfer in Fortran is with READ and WRITE and is called list-directed. More advanced forms of data transfer will be covered later and can be found in any standard Fortran 90 references.

1.1. Input

You have learned that variables can be given values by using numeric assignment statements, as in the following example to assign values to variables of BALANCE and RATE (interest rate):

BALANCE = 1000 RATE = 0.

This is an inflexible way of supplying data, since to run the program for different balances or interest rates you would have to find and change these statements. There may be many such assignments in a more complicated program, and it is a waste of time to recompile them every time you want to change the data. The READ* statement, however, allows you to supply the data while the program is running. Replace these two assignment statements with the single statement

READ (*, *) BALANCE, RATE

When you run the program, the compiler will wait for you to type the values of the two variables at the keyboard, if you are using a PC (an IBM compatible personal computer). They may be on the same line, separated by blanks or a comma, or on different lines. You can correct a number with the backspace key while entering it.

The general form of the READ statement is

READ (*, *) list

where list is a list of variables separated by commas.

Note the following general rules:

  • A single line of input or output is called a record (e.g., in the case of a PC, from the keyboard or on the screen).
  • Each READ statement requires a new input record, e.g., the statement

READ (*, *) A, B, C

will be satisfied with one record containing three values:

3 4 5

whereas the statements

READ (*, *) A

READ (*, *) B

READ (*, *) C

require three input records, each with one value in it:

3 4 5

  • When the compiler encounters a new READ, unread data on the current record is discarded, and the compiler looks for a new record to supply the data.
  • Data for a READ may run over onto subsequent records. Basically the compiler searches all input records for data until the I/O (Input/Output) list has been satisfied.
  • If there are not enough data to satisfy a READ statement, the program will crash with an error message.

The statements

READ (*, ) A READ (, ) B, C READ (, *) D

with the input records

1 2 3 4 7 8 9 10

have the same effect as the assignments

A = 1 B = 4 C = 7 D = 9

It often happens that you need to test a program by reading a lot of data. Suppose you were writing a program to find the average of, say, 10 numbers. It becomes a great nuisance to have to type in the 10 numbers each time you run the program (since programs seldom work correctly the first time:-) The following trick is very useful. The idea is to put the data in a separate (external) file, which is stored on your computer system, e.g., on its hard disk if you are using a PC. The program then reads the data from the file each time it is run, instead of from the PC keyboard. As an example, use your word processor to store the following line in the ASCII (text) file called DATA.TXT:

3 4 5

Now use this program to read these three numbers from the file and display them on the screen:

OPEN (1, FILE = NDATA.TXTN, STATUS=NOLDN)

organic matter content, which are obtained from a field study, or to sort a list of values, e.g., annual precipitation and crop yield, in frequency-magnitude analysis, or to solve a system of linear equations governing mass transport through a study domain. To avoid an enormously clumsy program, where perhaps hundreds of variable names are needed, we can use subscripted variables, i.e., arrays or dimensions. These may be regarded as variables with components, rather like vectors or matrices. They are written in the normal way, except that the subscripts are enclosed in parentheses after the variable name, e.g., X(3), Y(I + 2 * N). Fortran 90 has an extremely powerful set of array features, which have been significantly improved from FORTRAN 77.

2.1. Basic Rules and Notations

Array is our first example in Fortran 90 of a compound object, i.e., an object which can have more than one value. Arrays can be fairly complicated creatures. Only the basics are mentioned here. Interested users can self-learn the more advanced features with a little help of a Fortran 90 reference.

The statements

REAL : : X DIMENSION X(10)

declares X to be an array (or list) with 10 real elements, denoted by X(1), X(2), ..., X(10). The number of elements in an array is called its size (10 in this case). Each element of an array is a scalar (single-valued). Array elements are referenced by means of a subscript, indicated in parentheses after the array name. The subscript must be an integer expression—its value must fall within the range defined in the array declaration. So

X(I+1)

is a valid reference for an element of X as declared above, as long as (I+1) is in the range from 1 to 10. A compiler error occurs if the subscript is out of range.

By default, arrays have a lower bound of 1 (the lowest value a subscript can take). However, you can have any lower bound you like:

DIMENSION A(!1:100)

declares A to have 102 elements, from A(!1) to A(100). The upper bound must be specified; if the lower bound is missing it defaults to 1.

An array may have more than one dimension. The bounds of each dimension must be specified:

DIMENSION A(2, 3)

Here A represents a two-dimensional array. The number of elements along a dimension is called the extent in that dimension. So, array A has an extent of 2 in the first dimension, and 3 in the second dimension (and a size of 6). Fortran allows up to seven dimensions. The number of dimensions of an array is its rank, and the sequence of extents is its shape. The shape of A is (2, 3), or (2×3) in matrix notation. A scalar is regarded as having a rank of zero.

2.2. Assigning Values to an Array

An entire array may be read by the statement:

READ (*, *) X

Of course, the exact number of data values must be supplied. Note that if individual elements of an array are to be read by using multiple READ statements, then each data value would have to be on a separate line in a data file, which apparently is rather cumbersome.

A special case is that an entire array is assigned one scalar value with the statement X = 1.

2.3. An Example of Using Array

To illustrate the use of arrays, let us compute the population mean from a set of N observations. The mean is defined as

where Xi is the i th^ observation.

The following code computes this quantity from selected data read from the disk file DATA.TXT. The file contains N observations.

PROGRAM CMEAN IMPLICIT NONE

INTEGER I, N REAL X, XMEAN PARAMETER (N=956) DIMENSION X(N)

OPEN (1, FILE = NDATA.TXTN, STATUS = NOLDN)

READ (1, *) X

DO I = 2, N, 2 XMEAN = XMEAN + X(I) END DO

XMEAN = XMEAN / (N/2)

PRINT*, NMean = N, XMEAN

References

Hahn, B.D., Fortran 90 for Scientist and Engineers , Butterworth-Heinemann, Woburn, MA, 1994.