{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Basic_comphy_ch4

# Basic_comphy_ch4 - Ch 4 Fortran 90 Array References 1 T.M.R...

This preview shows pages 1–10. Sign up to view the full content.

1 Ch. 4 Fortran 90: Array References: 1. T.M.R. Ellis et al., “ Fortran 90 programming ” , Chs. 7, 13 2. L. Nyhoff & S. Leestma, “ Introduction to FORTRAN 90 ” , Ch. 8

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 Outline 4.1 The array concept 4.2 Array initialization 4.3 Input/Output with arrays 4.4 Arrays in computation 4.5 Allocatable arrays 4.6 Sub-arrays & array sections 4.7 Multidimensional arrays 4.8 Vectors & matrices
3 4.1 The array concept * In scientific computing, it is very common to manipulate ordered sets of values: Examples: Vectors v = v 1 i v 2 j v 3 k = v 1 , v 2 , v 3 Matrix A = { A 11 A 12 A 21 A 22 } B = { B 11 B 12 B 13 B 21 B 22 B 23 B 31 B 32 B 33 } * the elements (or components) all have the same name, but different subscript index. * Fortran provides the concept of array to deal with an ordered set of related variables. In Fortran, we use (for a one-dimensional array): v (1), v (2) , .... , v(n) name of the array subscript expression (integer enclosed in parenthese)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 Array declarations Type , DIMENSION( n ) :: array_1, array_2, array_3, .... Type = data type (eg, REAL , INTEGER ...) n (integer) = number of elements of each (one-dimensional) array in the list Examples: REAL, DIMENSION(3) :: v (an array having 3 elements) REAL, DIMENSION(10) :: a, b, c (each array has 10 elements) REAL :: a(10), b(10), c(10) (equivalent to above) REAL, DIMENSION(10) :: a, b, c(20), d ( a , b , d have 10 elements each; c has 20 elements) INTEGER, DIMENSION(20) :: m, n (each array has 20 elements (integer)) Remark: We shall discuss multi-dimensional array later.
5 * By default, the subscripts will start at 1: REAL, DIMENSION(n) :: v => the elements are: v(1), v(2), v(3),...v(n) * If we want to have a different range of values, we can use the lower bound & upper bound: REAL, DIMENSION(-1:1) :: v (elements = v(-1) , v(0) , v(1) ) REAL, DIMENSION(0:2) :: v (elements = v(0) , v(1) , v(2) ) REAL, DIMENSION(-3:-1) :: v (elements = v(-3) , v(-2) , v(-1) ) Examples:

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6 4.2 Array initialization * An array consists of a number of elements, it is necessary to provide values for each of these elements. REAL, DIMENSION(3) :: v REAL, DIMENSION (-2:0) :: a v(1) = 0.1 ; v(2) = 0.25 ; v(3) = 1.2 a(-2) = 0.12 ; a(-1) = 0.3 ; a(0) = 1.3 Examples: REAL, DIMENSION(10) :: a INTEGER, DIMENSION(10) :: b DO i =1, 10 a(i) = REAL(i) b(i) = i END DO (one-by-one) (via a DO loop)
7 array = (/ val_1 , val_2, ..., val_n /) Array constructor Example: REAL, DIMENSION(3) :: a INTEGER, DIMENSION(10) :: b a = (/ 1.2, 3.4, 2.5 /) b = (/ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 /) Note: Number of values must be equal to the number of elements of the array Question: What if the array has a large size (eg, 1000 elements)?

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
8 (var_list, count = initial, final, step ) Recall: count-controlled DO loop DO count = initial , final , step statement-sequence END DO Implied DO loop var_list : Body of the DO loop (list of variables or expressions) Example: INTEGER, DIMENSION(100) :: a INTEGER :: i a = (/ ( i , i=1,100 ) /) INTEGER, DIMENSION(100) :: a INTEGER :: i DO i = 1,100 a(i) = i END DO They are equivalent can be omitted if step = 1
9 * In general, var_list = var_1, var_2, var_3,...

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 47

Basic_comphy_ch4 - Ch 4 Fortran 90 Array References 1 T.M.R...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online