Presentation is loading. Please wait.

Presentation is loading. Please wait.

18 April, 2000 CS1001 Lecture 23 Quiz 5 Multi-Dimensional Array.

Similar presentations


Presentation on theme: "18 April, 2000 CS1001 Lecture 23 Quiz 5 Multi-Dimensional Array."— Presentation transcript:

1 18 April, 2000 CS1001 Lecture 23 Quiz 5 Multi-Dimensional Array

2 18 April, 2000 Quiz 5 -- #1 DO I = 1,5 IF (MOD(I,2) == 0) THEN Number(I) = 2 * I ELSE Number(I) = 2 * I+1 END IF END DO First time in loop: I = 1 MOD(1,2) = 1.NE. 0 so - ELSE Number(1) = 2*1+1=3 Second time in loop: I = 2 MOD(2,2) = 0.EQ. 0 so - THEN Number(2) = 2*2 = 4 Third time in loop: I = 3 MOD(3,2) = 1.NE. 0 so - ELSE Number(1) = 2*3+1=7 Fourth time in loop: I = 4 MOD(4,2) = 0.EQ. 0 so - THEN Number(2) = 2*4 = 8 Fifth time in loop: I = 5 MOD(5,2) = 1.NE. 0 so - ELSE Number(1) = 2*5+1=11

3 18 April, 2000 Quiz 5 -- #2 I = 0 DO I = I + 1 Number (I) = MIN ( I,3) IF (I >= 5) EXIT END DO Third time in loop: I = 2 + 1 = 3 Number (3) = MIN ( 3,3) = 3 (3 >= 5) is.FALSE. Do not EXIT Fourth time in loop: I = 3 + 1 = 4 Number (4) = MIN ( 4,3) = 3 (4 >= 5) is.FALSE. Do not EXIT Fifth time in loop: I = 4 + 1 = 5 Number (5) = MIN ( 5,3) = 3 (5 >= 5) is.TRUE. EXIT I = 0 First time in loop: I = 0 + 1 = 1 Number (1) = MIN ( 1,3) = 1 (1 >= 5) is.FALSE. Do not EXIT Second time in loop: I = 1 + 1 Number (2) = MIN ( 2,3) = 2 (2 >= 5) is.FALSE. Do not EXIT

4 18 April, 2000 Multi-Dimension Arrays Basically the same as one-dimensional –Declared as: REAL, DIMENSION(4, 3, 7) :: Temp or REAL, DIMENSION(1:4, 1:3, 1:7) :: Temp or REAL :: Temp(1:4, 1:3, 1:7) –An element is Temp(1, 3, 2) Multi-Dimensional Array Apps –Tables of data –Matrix processing

5 18 April, 2000 2-Dimensional Array Declare and dimension INTEGER,DIMENSION(1:9,1:3):: iVar1, iVar2 REAL, DIMENSION(0:25,1:5) :: rVar1, rVar2 Initialize whole array iVar1 = 0 rVar1 = 9.99 Reference elements using subscript iVar2(8,2) = 25 rVar2(19,5) = 17.49

6 18 April, 2000 Array Processing -- storage Rowwise: Table(1,1) Table(1,2) Table(1,3) Table(2,1) Table(2,2) etc. Columnwise Table(1,1) Table(2,1) Table(3,1) Table(1,2) Table(2,2) etc. Fortran convention is columnwise (1,1) (1,2) (1,3) (2,1) (2,2) (2,3) ? :

7 18 April, 2000 Matrix Processing m x n matrix addition and subtraction is done element by corresponding element INTEGER :: M(2,3),N(2,3),Add(2,3),Sub(2,3) ! Remember this is done columnwise! READ *, M READ *, N ! Let the computer do the work and handle all ! of the overhead: Add = M + N Sub = M - N PRINT *, Add PRINT *, Sub ! The output isn’t pretty, but it’s right M(1,1) M(2,1) M(1,2) M(2,2) M(1,3) M(2,3) Add(1,1) Add(2,1) Add(1,2) Add(2,2) Add(1,3) Add(2,3)

8 18 April, 2000 Matrix Addition INTEGER :: M(2,3),N(2,3),Add(2,3),Sub(2,3), I,J ! READ elements of the 2 arrays! DO I = 1,2 DO J = 1,3 READ *, M(I,J) END DO DO I = 1,2 DO J = 1,3 READ *, N(I,J) END DO Input: M(1,1) M(1,2) M(1,3) M(2,1) M(2,2) M(2,3) N(1,1) N(1,2) N(1,3) N(2,1) N(2,2) N(2,3) ! Implied Loop DO I = 1,2 READ *, ((M(I,J), J=1,3) END DO Input: M(1,1) M(1,2) M(1,3) M(2,1) M(2,2) M(2,3)

9 18 April, 2000 Matrix Add/Subtract (Cont’d) ! Add M and N, Subtract N from M DO I = 1,2 DO J = 1,3 Add(I,J) = M(I,J)+N(I,J) END DO DO I = 1,2 DO J = 1,3 Add(I,J) = M(I,J)-N(I,J) END DO ! Print results using Implied Do-Loop DO I = 1,2 PRINT *, (Add(I,J), J=1,3) END DO DO I = 1,2 PRINT *, (Sub(I,J), J=1,3) END DO I = 1: J=1: Add(1,1) = M(1,1)+N(1,1) J=2: Add(1,2) = M(1,2)+N(1,2) J=3: Add(1,3) = M(1,3)+N(1,3) I = 2: J=1: Add(2,1) = M(2,1)+N(2,1) J=2: Add(2,2) = M(2,2)+N(2,2) J=3: Add(2,3) = M(2,3)+N(2,3) I = 1: Add(1,1) Add(1,2) Add(1,3) I = 2: Add(2,1) Add(2,2) Add(2,3)

10 18 April, 2000 Matrix Multiplication The number of columns of A must be equal to the number of rows of B to be able to multiply them 22 = 1*4 + 0*6 + 2*9 2 = 1*2 + 0*4 + 2*0 48 = 3*4 + 0*6 + 4*9 6 = 3*2 + 0*4 + 4*0 1 0 2 3 0 4 4 2 5 3 6 4 1 8 9 0 0 2 22 2 5 7 48 6 15 17 *=

11 18 April, 2000 Intrinsic Array-Processing Subprograms MATMUL(A,B) - returns the matrix product of matrices A and B. MAXVAL(A,D) - returns an array of one fewer dimension than A containing the max values in array A along dimension D. If D is omitted, the max value of the whole array is returned. For example: 4 2 5 3 6 4 1 8 9 0 0 2 A= MAXVAL(A,2) = 589589 9 4 5 8 MAXVAL(A,1) = MAXVAL(A) = 9

12 18 April, 2000 PROGRAM Finding_Row_Maxima_1 !----------------------------------------------------- ! Program to read an array of numbers and to find ! the maximum value in ! each row using the subroutine FindRowMaxima. ! Identifiers used are: ! IntArray : two-dimensional array of numbers ! NumRows : number of rows in IntArray (constant) ! NumColumns : number of columns in IntArray (constant) ! FindRowMaxima : subroutine to find the row maxima ! I, J : subscripts ! ! Input: NumRows, NumColumns, and IntArray ! Output: The maximum value in each row ! (using FindRowMaxima) !--------------------------------------------------

13 18 April, 2000 IMPLICIT NONE INTEGER, PARAMETER :: NumRows = 4, NumColumns = 5 INTEGER, DIMENSION(NumRows, NumColumns) :: IntArray INTEGER :: I, J PRINT *, "Enter the", NumRows, "x", NumColumns, & "array of integers in a rowwise manner:" READ *, ((IntArray(I,J), J = 1, NumColumns), & I = 1, NumRows) CALL FindRowMaxima(IntArray) CONTAINS Enter the 4 x 5 array of integers in a rowwise manner: 1 2 3 4 5 6 6 6 6 6 4 3 2 1 0 2 9 4 8 6

14 18 April, 2000 !-FindRowMaxima---------------------------------- ! Subroutine to find and display the maximum value ! in each row of an array Table of integers. ! Local identifiers used are: ! Rows : number of rows in array Table ! (named constant) ! Cols : number of columns in array Table ! (named constant) ! RowMax : one-dimensional array of row maxima ! I : subscript ! ! Accepts: Array Table ! Output: The maximum value in each row of Table !-------------------------------------------------

15 18 April, 2000 SUBROUTINE FindRowMaxima(Table) INTEGER, PARAMETER :: Rows = 4, Cols = 5 INTEGER, DIMENSION(Rows, Cols), INTENT(IN) :: Table INTEGER, DIMENSION(Rows) :: RowMax INTEGER :: I ! Find max values along dimension 2 RowMax = MAXVAL(Table, 2) DO I = 1, Rows PRINT 130, I, RowMax(I) 130 FORMAT (1X,"Maximum entry in row", I2, "is", I2) END DO END SUBROUTINE FindRowMaxima END PROGRAM Finding_Row_Maxima_1 Maximum entry in row 1 is 5 Maximum entry in row 2 is 6 Maximum entry in row 3 is 4 Maximum entry in row 4 is 9


Download ppt "18 April, 2000 CS1001 Lecture 23 Quiz 5 Multi-Dimensional Array."

Similar presentations


Ads by Google