2: Arrays, Loops and Mixed-mode Expressions.

2.1 Arrays

Arrays are very useful and flexible storage space for numbers where they are in a sequence such as in vectors and matrices. Each element of the array is an element in the vector or matrix.

Arrays must be declared at the beginning of the program so that the computer knows how much space to reserve for them.

eg	DIMENSION VECT(30),MATRX(30,30)

The declaration DIMENSION is followed by a space, and then the array list separated by commas. The arrays VECT and MATRX are declared respectively as a one-dimensional array of size 30 elements, and a two-dimensional array of size 30 by 30. Note the second array begins with the letter M, so it is an INTEGER array.

The array VECT has 30 storage locations for real numbers associated with it, from VECT(1) to VECT(30). MATRX has 900 integer storage locations.

It is possible to explicitly declare the arrays as being type integer or real by using the declarations INTEGER and REAL. This overrides the default type, however, it is not good practice to do this as it may be confusing when trying to follow the program in debugging for example.

For complex arrays they must be declared explicitly anyway.

eg	REAL VECT(30)
	INTEGER MATRX(30,30)
	COMPLEX SCATT(10,10,10)

Array SCATT has 1000 storage locations for the real parts and 1000 locations for the imaginary parts of the complex numbers.

REAL MATRX(30,30) would force MATRX array to be real.

To use an array, it is used just like a variable except the precise element of the array has to be specified by using a subscript. The subscript must be integer, expressions are allowed. Thus:

	VECT(1)
	MATRX(5,6)
	MATRX(I,J)
	MATRX(I+1,2*J-1)

are all valid subscripts, provided their values DO NOT exceed the original limits in the declaration. VECT(31) for example is out of bounds in this case, and so is VECT(0). However, it is possible to make the limits allow subscripts less than one, by specifying the limits in the form lower limit:upper limit.

eg	DIMENSION REFLEC(0:100),TRANSM(-5:5,-5:5)

2.2 Loops

There are many occasions when a calculation needs to be repeated a certain number of times, perhaps on different elements of a matrix; or two matrices need to be multiplied together, where an automatic loop would be very useful.

FORTRAN has such a loop - the DO loop. The full statement is:

	  DO label variable=start,finish,increment
	  statements
	  .
	  .
 label  CONTINUE

   eg
      DIMENSION NUM(10)
      DO 10 I=1,10,1
        NUM(I)=I
  10  CONTINUE

This would put 1 into NUM(1), 2 into NUM(2), ... 10 into NUM(10)

In fact, if the increment is one then it need not be specified. The increment is assumed to be one unless otherwise given.

You are allowed to use variables or expressions as the start, finish, and increment terms, but changing the value of these during the loop has no effect as the number of times the loop is executed is calculated at the start of the DO loop and stored by the computer. Note the following cases:

           DO 100 J=1,1                   DO 200 K=1,10,-1
           .                              .
           .                              .
       100 CONTINUE                   200 CONTINUE

The left DO loop will be executed once. The right DO loop will not be executed.

The number of times a DO loop is executed is determined by the computer: it is the maximum of either ((final-start+increment)/increment) or zero.

You are able to jump out of a DO loop if needed by use of GO TO; but you are NOT allowed to jump into the middle of a DO loop from outside it. Also, you are allowed to have a DO loop within a DO loop - but they must be nested correctly.

2.3 Mixed-mode expressions

The rules of FORTRAN allow an arithmetic expression to contain variables and constants of different data types (integer ,real, complex, etc) - a so called mixed-mode expression. Under these rules the data type with less precision will be converted to the higher precison data type before the arithmetic operation. The result will also be of the higher precision data type. Thus:

	R=A*M

The integer data in variable M will be converted to real data before being multiplied by A. The result will be a real number stored in R.

In converting from a real to integer the number is truncated NOT rounded to the nearest integer. Thus:

	I=2.8

will store 2 in integer variable I.

Note that the division of one integer by another will result in an integer result. Thus:

	RESULT=5/4

will give RESULT a value of 1.0000, since the result of the division is integer, and then this is converted to real for storage in real variable RESULT.

To overcome this, one can simply write:

	RESULT=5./4. 
(the constants are now real numbers)

or, in the case of integer variables, explicitly convert them to real before the division. Thus:

	RESULT=FLOAT(J)/FLOAT(K)

Other useful functions:

	FLOAT(I)     convert integer I to a real (floating point) number
	INT(R)       convert real R to integer by truncation
	NINT(R)      convert real R to nearest whole integer
	CMPLX(R)     convert real R to complex with R as the real part and 0 imaginary.
	CMPLX(R,S)   convert real R to complex: R real part, S imaginary part.
	ABS(R)       absolute value of R (real or integer: for complex R gives real magnitude of R)