Introduction to Programming with Fortran ---------------------------------------- Practical Exercise 4 -------------------- Question 1 ---------- 4.1 Give the rank, bounds, size and shape of the arrays defined as follows: a) REAL, DIMENSION(10) :: ONE b) REAL, DIMENSION(2, 0:2) :: TWO c) INTEGER, DIMENSION(-1:1, 3 , 2) :: THREE d) REAL, DIMENSION(0:1, 3) :: FOUR Question 2: Conformance ----------------------- 4.2 Given: a) REAL, DIMENSION(1:10) :: ONE b) REAL, DIMENSION(2, O:2) :: TWO c) INTEGER, DIMENSION(-1:1, 3, 2) :: THREE d) REAL, DIMENSION(0:1, 3) :: FOUR Which two of the arrays are conformable? Question 3: Hotel Array ----------------------- 4.3 Declare an integer array of rank 3 which might be suitable for representing a hotel with 3 buildings, each with 8 floors and 16 rooms on each floor, and write an assignment of the value 3 to the 5th room on floor 7 of the second building. Question 4: Array References ---------------------------- 4.4 Given: INTEGER :: i = 3, j = 7 REAL, DIMENSION(1:20) :: A which of the following are valid array references for the array: a) A(12) b) A(21) c) A(I*J-1) d) A(3.0) e) A(I*J) f) A(1+INT(4.0*ATAN(1.0))) [ Hint: 4.0*ATAN(1.0) is pi ] Question 5: Array Element Ordering ---------------------------------- 4.5 Given: a) REAL, DIMENSION(1:10) :: ONE b) REAL, DIMENSION(2, 0:2) :: TWO c) INTEGER, DIMENSION(-1:1, 3, 2) :: THREE d) REAL, DIMENSION(0:1, 3) :: FOUR write down the array element order of each array. Question 6: Array Sections -------------------------- 4.6 Declare an array which would be suitable for representing draughts board. Write three assignments to set all the white squares to zero and the black squares to unity. [ A draughts board is 8 x 8 with alternate black and white squares, in both directions. ] Question 7: Array Constructor ----------------------------- 4.7 Write an array constructor for the rank one 5 element array BOXES containing the values 1, 4, 6, 12, 23. Question 8: MATMUL Intrinsic ---------------------------- For the declarations: REAL, DIMENSION(100, 100) :: A, B, C REAL, DIMENSION(100, 500) :: P, Q, R REAL, DIMENSION(500, 100) :: T 4.8.1 What is the difference between C = MATMUL(A,B) and C = A*B? 4.8.2 Which of the following are correct? a) A = MATMUL(P,T) b) P = MATMUL(Q,R) c) A = P*T d) P = Q*R e) T = MATMUL(T,A) Question 9: MATMUL By Hand -------------------------- In this question, you may assume that there are no errors and omit all checking. That is not good practice, but considerably simplifies the coding. You should not assume the matrices are square. You may also assume that all lower bounds are one. The harness for a sort of MATMUL could be: SUBROUTINE MyMatmul (result, left, right) IMPLICIT NONE REAL, DIMENSION :: result, left, right . . . END SUBROUTINE MyMatmul 4.9.1 Replace the ". . ." by the statements needed to calculate the matrix (not elementwise) product `result = left*right' using basic scalar operations only. 4.9.2 Replace the ". . ." by the statements needed to calculate the matrix (not elementwise) product result = left*right using the SUM intrinsic. Question 10: ALLOCATABLE Arrays ------------------------------- 4.10.1 You have a subroutine like the following: SUBROUTINE OPERATE (ARG) IMPLICIT NONE REAL :: ARG(:, :) . . . END SUBROUTINE OPERATE Replace the ". . ." by the statements needed to allocate an array with the same shape as ARG. Do this using automatic arrays, not using ALLOCATE. You may assume that all lower bounds are one. 4.10.2 Repeat 10.1 but use ALLOCATE, not automatic arrays. Question 11: Actual Coding -------------------------- 4.11 You have a subroutine like the following: SUBROUTINE OPERATE (ARG, N, ICODE) IMPLICIT NONE REAL :: ARG(:, :) INTEGER :: N, ICODE . . . END SUBROUTINE OPERATE Replace the ". . ." by the statements needed to do all of the following: check that N is positive or zero check that the matrix has at least one element check that the matrix is square allocate an array with the same shape as ARG using ALLOCATE replace ARG by ARG**N, but using matrix multiplication (i.e. MATMUL) and not element-wise multiplication You may assume that all lower bounds are one.