140 likes | 261 Views
Programming For Nuclear Engineers Lecture 6 Arrays. Introduction 1- Declaring Arrays 2- Using Array Elements in Fortran Statements 2.1- Initialization of Array Elements 3- Input and Output 3.1- Input and Output of Array Elements 3.2 -The Implied DO Loop
E N D
Introduction 1- Declaring Arrays 2- Using Array Elements in Fortran Statements 2.1- Initialization of Array Elements 3- Input and Output 3.1- Input and Output of Array Elements 3.2 -The Implied DO Loop 3.3 -I/O of whole arrays and array sections
Introduction An array is a group of variables or constants, all of the same type, that are referred to by a single name. Arrays can be extremely powerful tools. They permit us to apply the same algorithm over and over again to many different data items with a simple DO loop. For example, suppose that we need to take the sine of 100 different real numbers. If the numbers are stored as elements of any array a consisting of 100 real values, then the code DO i = 1, 100 a(i) = sin(a(i)) END DO
1- Declaring Arrays The type and the number of elements that the array contains must be declared to the compiler in a type declaration statement. The size of the array being defined is declared by using the DIMENSION attribute. For example, a real array flux containing 20 elements could be declared as follows: REAL, DIMENSION(20) :: flux or REAL :: flux(20)
- The number of elements in a given dimension of an array is called the extent of the array in that dimension. - The number of subscripts declared for a given array is called the rank of the array. - The combination of the rank and the extent of the array, is called the shape of an array. - The size of an array is the total number of elements declared in that array. - An Array Constant, is an array consisting entirely of constants. It is defined by placing the constant values between special delimiters, called array constructors. For example, the expression shown below defines an array constant containing five integer elements: (/1,2,3,4,5/) Fortran 95 [1,2,3,4,5] Fortran 2003
2- Using Array Elements in Fortran Statements Array elements may be included in arithmetic and logical expressions, and the results of an expression may be assigned to an array element. Each element of an array is a variable just like any other variable, and an array element may be used in any place where an ordinary variable of the same type may be used. For example, assume that arrays index and temp are declared as: INTEGER, DIMENSION(10)::index REAL, DIMENSION(3)::temp Then the following Fortran statements are valid: Index(1)=5 temp(3)= REAL(index(1))/4. WRITE(*,*)’index(1) = ‘ , index(1)
2.1 Initialization of Array Elements The values in an array must be initialized before use. If an array is not initialized, the contents of the array elements are undefined. The elements in an array may be initialized by one of three techniques: By using assignment statements. In type declaration statements at compilation time. By using READ statements. 1. Assignment statements the following examples will initialize the elements of array1: REAL, DIMENSION(10)::array1 DO i= 1,10 DO loop will initialize the elements of array1(i)=REAL(i) array array1 to 1.0,2.0,3.0,etc. END DO
using an array constructor will accomplish the same function all at once: REAL, DIMENSION(10)::array1 array1= (/1.,2.,3.,4.,5.,6.,7.,8.,9.,10./) 2.Type declaration statements To initialize an array in a type declaration statement, we use an array constructer to declare its initial value in that statement. INTEGER, DIMENSION(5)::array1=(/1,2,3,4,5/) To initialize larger arrays, we can use an implied DO loop, which have the general form: (arg1,arg2,…,index=istart,iend,incr) Where arg1, arg2,etc., are values evaluated each time the loop is executed. The variable index is set to the value istart. index is incremented by incr in each successive loop until its index*incr>iend*incr, at which time the loop terminates
3. READ statements Arrays may also be initialized with READ statements. The use of arrays in I/O statements will be described in the next section. 3- Input and Output 3.1 Input and Output of Array Elements READ and WRITE statements containing array elements are just like READ and WRITE statements for any other variables. 3.2 The Implied DO Loop It allows an argument list to be written many times as a function of an index variable. The general form of a WRITE or READ statement with an implied DO loop is: WRITE (unit,format)(arg1,arg2,…,index=istart,iend,incr) READ (unit,format) (arg1,arg2,…,index=istart,iend,incr)
For the following example, what will be the output? 1- WRITE(*,100) (i, 2*i, 3*i, i = 1,3) 1000 FORMAT (1X, 9I6) 2- WRITE (*,100) ((i, j, j = 1, 3),i = 1, 2) 100 FORMAT (1X, I5, 1X, I5) 3- WRITE (*,100) (a(i), i = 1, 5) 100 FORMAT (1X, ‘a = ‘ , 5F10.2) 4- n_factorial = 1 DO i = 1, n n_ factorial = n_factorial * i END DO
1- 1 2 3 2 4 6 3 6 9 2- 1 1 1 2 1 3 2 1 2 2 2 3 3- 1.00 2.00 3.00 4.00 5.00 4- depend on the value of n, if n is 10, the DO loop parameters will be istart = 1, iend = 10, and incr = 1, so the resulting value of n_factorial will be 1x2x3x4x5x6x7x8x9x10 = 3628800
3.3 I/O of whole arrays and array sections Entire arrays or array sections may also be read or written with READ and WRITE statements. The following example how we could use an array and two array sections in I/O statements. PROGRAM array_io … … IMPLICIT NONE ! Data dictionary: declare variable types &definitions REAL, DIMENSIONS(5)::a=(/1.,2.,3.,20.,10./) INTEGER,DIMENSION(4)::vec=(/4,3,4,5/) !Output entire array. WRITE(*,100)a 100 FORMAT(2X,5F8.3)
!Output array section selected by a triplet. WRITE (*,100)a(2::2) !Output array section selected by a vector subscript. WRITE(*,100)a(vec) END PROGRAM array_io The output from this program is: 1.000 2.000 3.000 20.000 10.000 2.000 20.000 20.000 3.000 20.000 10.000 WHY?