1 / 38

CHAPTER 9 MUTIDIMENSIONAL ARRAYS

CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables. Compile-Time Arrays & Run-Time Arrays. Compile-Time Arrays : The size is fixed before execution begins.

Download Presentation

CHAPTER 9 MUTIDIMENSIONAL ARRAYS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CHAPTER 9 MUTIDIMENSIONAL ARRAYS

  2. Introduction to multidimensional Arrays and Multiply subscripted variables

  3. Compile-Time Arrays & Run-Time Arrays • Compile-Time Arrays: The size is fixed before execution begins. • Run-Time (or Allocatable 可分配的) Arrays: The memory is allocated (分配) during execution, making it possible to allocate an array of appropriate size.

  4. Compile-Time Arrays REAL, DIMENSION(4, 3)::Temperature REAL, DIMENSION(1:4, 1:3):: Temperature Temperature (2, 3 ) 64.5 Temperature (I, J) REAL, DIMENSION(4, 3, 7):: TemperatureArray REAL, DIMENSION(1:4, 1:3, 1:7)::TemperatureArray TemperatureArray (1, 3, 2) → 64.3 TemperatureArray (Time, Location, Day) →

  5. Compile-Time Arrays REAL, DIMENSION(1:2, -1:3)::Gamma Gamma(1, -1), Gamma(1,0), Gamma(1,1), Gamma(1, 2), Gamma(1,3), Gamma(2, -1), Gamma(2,0), Gamma(2,1), Gamma(2, 2), Gamma(2,3) REAL, DIMENSION (0:2, 0:3, 1:2) :: Beta INTEGER, DIMENSION(5:12) :: Kappa

  6. Declaration of Compile-Time Array type, DIMENSION(l1:u1, l2:u2 ,‧‧‧lk:uk) :: & list-of-array-names li:ui The specified lower limit li through the upper limit ui. The number k of dimensions, called the rank (秩) of array, is at most seven.

  7. Declaration of Allocatable Array type, DIMENSION(:, :,‧‧‧:), & ALLOCATABLE :: list type, DIMENSION(:, :,‧‧‧:) :: list ALLOCATABLE :: list The rank k of the array (the number of dimensions) is at most seven.

  8. Allocatable Array (可分配的) / Run-Time Arrays REAL, DIMENSION(:, :, :), ALLOCATABLE :: & Beta REAL, DIMENSION(:, :), ALLOCATABLE :: & Gamma

  9. ALLOCATE Statement • ALLOCA TE (list) • ALLOCATE (list, STAT = status-variable) where list is a list of array specifications of the form array-name (l1:u1, l2:u2 ,‧‧‧lk:uk) ALLOCATE (Beta(0:2, 0:3, 1:2), Gamma & (1:N, -1:3), STAT = AllocateSatus) DEALLOCATE(***)

  10. Input/Output of Multidimensional Arrays • Element-wise Processing row (列) × column (行) • Two natural orders for processing the elements of a two-dimensional array: row-wise and column-wise. • In most cases, a programmer can select one of these orderings by controlling the way the subscripts (下標) vary. If this is not done, the Fortran convention is that two-dimensional arrays will be processed column-wise.

  11. (a) Row-wise Processing (b) Column-wise Processing

  12. Processing a Three-Dimensional Array (2×4×3)

  13. Input/Output of Array Elements • Using a DO loop • Using the array name • Using an implied DO loop • Using an array section INTEGER, DIMENSION (3, 4) :: Table

  14. Input/Output Using DO Loops INTEGER, DIMENSION (3, 4) :: Table DO Row = 1, 3 DO Col =1, 4 READ *, Table (Row, Col) END DO END DO

  15. Input/Output Using DO Loops INTEGER, DIMENSION (3, 4) :: Table DO Col =1, 4 DO Row = 1, 3 READ *, Table (Row, Col) END DO END DO DO Row = 1, 3 DO Col =1, 4 PRINT *, Table (Row, Col) END DO END DO

  16. Input/Output Using the Array Name INTEGER, DIMENSION (3, 4) :: Table READ *, Table 77, 99, 48, 56, 10, 89 32, 100, 77, 25, 46, 33 PRINT ‘(1X, 4I5/)’ Table

  17. Input/Output Using Implied DO Loops INTEGER, DIMENSION (3, 4) :: Table READ *, ((Table (Row, Col), Col =1, 4 ), & Row = 1, 3) READ *, (Table (Row,1), Table (Row,2), & Table (Row,3), Table (Row,4), & Row = 1, 3)

  18. Input/Output Using Implied DO Loops READ *, ((Table (Row, Col), Row = 1, 3) ), & Col =1, 4) READ *, (((B(I, J, K), I = 1, 2), J =1, 4), & K = 1, 3)

  19. Input/Output Using Implied DO Loops DO Row = 1, 3 PRINT ‘(1X, 4I5)’ , (Table (Row, Col), Col =1, 4) END DO

  20. Examples Figure 9.3, p.628 • Temperature Table • Rate is a 3 × 4 array

  21. Examples: p. 630 • READ *, N, (Number (I), I =1, N), M, & ((Rate (I,J), J = 1, N), I = 1, M) 4 16, 37, 76, 23 3 16.1, 7.3, 18.4, 6.5 0.0, 1.0, 1.0, 3.5 18.2, 16.9, 0.0, 0.0

  22. Examples: p. 630 PRINT 5, (“Row”, I, (Rate (I,J), J= 1, 4), I = 1, 3) 5 FORMAT (1X, A, I2, “--”, 4F6.1/) Row 1-- 16.1 7.3 18.4 6.5 _________________________ Row 2-- 0.0 1.0 1.0 3.5 _________________________ Row 3-- 18.2 16.9 0.0 0.0 _________________________

  23. Examples: p. 630 PRINT 6, (J, (Rate (I,J), I = 1, 3), & Number (J), J= 1, 4), “Total”, Total 6 FORMAT (4(1X, I4, 5X, 3F6.1, I10/), A, T35, I3) 1 16.1 0.0 18.2 16 2 7.3 1.0 16.9 37 3 18.4 1.0 0.0 76 4 6.5 3.5 0.0 23 Total 152

  24. 9.3 Processing Multidimensional Arrays • Array Constants INTEGER, DIMENSION (2, 3) :: A A = RESHAPE ((/ 11, 22, 33, 44, 55, 66 /), (/ 2, 3 /)) or A = RESHAPE ((/ (11*N, N =1, 6) /), (/ 2, 3 /) Reshape (v.) 重塑Shape (n. v.) 形狀

  25. Array Constants A = RESHAPE ((/11, 22, 33, 44, 55, 66 /), & (/ 2, 3 /), ORDER = (/2, 1/)) • The order (/2, 1/) specifies that the second subscript (下標) is to be varied before the first, which causes the array to be filled row-wise.

  26. Array Constants A = RESHAPE ((/11, 22, 33, 44 /), (/ 2, 3 /), & PAD = (/0, 0/), ORDER = (/2, 1)) pad (v.) 填充

  27. Array Constants • The intrinsic function SHAPE can be used to determine the shape of an array, which consists of number of dimensions for array and the extent (the number of subscripts下標之大小程度) in each dimension. • For example, SHAPE (A) will return (2, 3). Shape 形狀(n.); 塑造 (v.)

  28. Array Expressions (p. 636) &Array sections and Subarrays • INTEGER, DIMENSION (2, 3) :: A • A(1:2:1, 2:3:1) or A(:, 2:3)

  29. Array sections and Subarrays • A(2, 1:3:1) or A(2, :) • A((/ 2, 1 /), 2:3)

  30. Array Assignment INTEGER, DIMENSION (2, 3) :: A INTEGER, DIMENSION (3, 2) :: B A = 0 B = RESHAPE (A, (/3, 2/))

  31. Array Assignment A(:, 2:3) = RESHAPE ((/ (I**2, I = 1, 4) /), & (/2, 3/))

  32. Array Assignment: Example REAL, DIMENSION (2, 3) :: Alpha, Beta WHERE (Alpha /= 0.0) Beta = 1.0 / Alpha ELSEWHERE Beta = 0.0 END WHERE

  33. Intrinsic Array-Processing Subprograms

  34. Matrix Processing (Sec. 9.6) &Intrinsic Array-Processing Subprograms • MATMUL (A, B) --- The product AB • TRANSPOSE (A)

  35. Application: Pollution Tables • In a certain city, the air pollution is measured at a two-hour intervals, beginning at midnight. These measurements are recorded for a one-week period and stored in a file, the first line of which contains the pollution level for day 1, the second line for day 2, and so on. • A program must be written to produce a weekly report that displays the pollution levels in a table of the form:

  36. Monitoring Air Pollution

More Related