160 likes | 328 Views
LAB-12 2-D Array. I Putu Danu Raharja raharja @kfupm.edu.sa Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals. 2-D Array. Most common use is to manipulate matrices Stored contiguously in memory like 1-D arrays (column-wise)
E N D
LAB-122-D Array I Putu Danu Raharja raharja @kfupm.edu.sa Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals
2-D Array • Most common use is to manipulate matrices • Stored contiguously in memory like 1-D arrays (column-wise) • PRINT and READ operation without subscript means column-wise
Declaration • Two ways • TYPE ANAME (R, C) → Explicit • DIMENSION ANAME (R, C) → Implicit
Initialization • Assignment • Row-major • Column-major • User input • Row-major • Column-major
Row-major Assignment INTEGER IDENT (3, 3), ROW, COL DO 100 ROW = 1, 3 DO 100 COL = 1, 3 IF (ROW .EQ. COL) THEN IDENT(ROW, COL) = 1 ELSE IDENT(ROW, COL) = 0 ENDIF 100 CONTINUE PRINT*,IDENT END
Column-wise Assignment Example INTEGER IDENT (3, 3), ROW, COL DO 100 COL = 1, 3 DO 100 ROW = 1, 3 IF (ROW .EQ. COL) THEN IDENT(ROW, COL) = 1 ELSE IDENT(ROW, COL) = 0 ENDIF 100 CONTINUE PRINT*, IDENT END
Row-wise READ Example (1) INTEGER MAT (2, 3), ROW, COL DO 100 ROW = 1, 2 DO 100 COL = 1, 2 READ*, MAT (ROW, COL) 100 CONTINUE PRINT*, MAT END
Row-wise READ Example (2) INTEGER MAT (2, 3), ROW, COL DO 100 ROW = 1, 2 READ*, (MAT (ROW, COL), COL = 1, 3) 100 CONTINUE PRINT*, MAT END
Row-wise READ Example (3) INTEGER MAT (2, 3), ROW, COL READ*, ((MAT (ROW, COL), COL = 1, 3), ROW = 1, 2) PRINT*, MAT END
Column-wise READ Example (1) INTEGER MAT (2, 3), ROW, COL DO 100 COL = 1, 2 DO 100 ROW = 1, 3 READ*, MAT (ROW, COL) 100 CONTINUE PRINT*, MAT END
Column-wise READ Example (2) INTEGER MAT (2, 3), ROW, COL DO 100 COL = 1, 2 READ*, (MAT (ROW, COL), ROW = 1, 3) 100 CONTINUE PRINT*, MAT END
Column-wise READ Example (3) INTEGER MAT (2, 3), ROW, COL READ*, ((MAT (ROW, COL), ROW = 1, 2), COLUMN = 1, 3) END
Row-wise PRINT Example (1) DO 100 ROW = 1, 2 DO 100 COL = 1, 3 PRINT*, MAT (ROW, COL) 100 CONTINUE
Row-wise PRINT Example (2) DO 100 ROW = 1, 2 PRINT*, (MAT (ROW, COL), COL = 1, 3) 100 CONTINUE
Row-wise PRINT Example (3) PRINT*, ((MAT (ROW, COL), COL = 1, 3), ROW = 1, 2)
Exercises • Read an Integer 2x3 matrix MAT. Calculate the sums of elements of each column and store the sums in a 1-D array TOTAL. Print MAT row-wise (2 lines) and TOTAL (1 line). • Write a function to check if two 3x2 matrices are identical and show appropriate messages. Write a main program to test the function. • Read two integer 2x2 matrices LMAT and RMAT. Calculate and show the multiplication of LMAT and RMAT. • Write a function to transpose a MxN matrix. Write a main program to test the function.