Array Processing { Part II. Multi-Dimensional Arrays. 1. What is a multi-dimensional array?

Similar documents
Subroutines, Functions and Modules

NO CALCULATOR ALLOWED!!

Matrices. Jordi Cortadella Department of Computer Science

Computer Science & Engineering 150A Problem Solving Using Computers

Arrays. Arrays are of 3 types One dimensional array Two dimensional array Multidimensional array

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013

1 Short Answer (10 Points Each)

CSCE 110 PROGRAMMING FUNDAMENTALS. Prof. Amr Goneid AUC Part 7. 1-D & 2-D Arrays

Matrix Multiplication

Matrix Multiplication

AN INTRODUCTION TO FORTRAN 90 LECTURE 2. Consider the following system of linear equations: a x + a x + a x = b

BITG 1113: Array (Part 1) LECTURE 8

CSE 230 Intermediate Programming in C and C++ Arrays and Pointers

HPF commands specify which processor gets which part of the data. Concurrency is defined by HPF commands based on Fortran90

Lecture 5: Matrices. Dheeraj Kumar Singh 07CS1004 Teacher: Prof. Niloy Ganguly Department of Computer Science and Engineering IIT Kharagpur

Where does the insert method place the new entry in the array? Assume array indexing starts from 0(zero).

Introduction to Fortran Programming. -Internal subprograms (1)-

Maltepe University Computer Engineering Department. BİL 133 Algorithms and Programming. Chapter 8: Arrays

BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18)

ARRAYS COMPUTER PROGRAMMING. By Zerihun Alemayehu

Declaration and Initialization

CS 33. Caches. CS33 Intro to Computer Systems XVIII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Parallelizing The Matrix Multiplication. 6/10/2013 LONI Parallel Programming Workshop

Review More Arrays Modules Final Review

Matrices. A Matrix (This one has 2 Rows and 3 Columns) To add two matrices: add the numbers in the matching positions:

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers

Arrays in C. By Mrs. Manisha Kuveskar.

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #04

Lab #10 Multi-dimensional Arrays

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

Linear Loop Transformations for Locality Enhancement

Lesson 9: Introduction To Arrays (Updated for Java 1.5 Modifications by Mr. Dave Clausen)

CS1073 Exam 3, Fall 2009 page 1. Important test instructions code fragments

7. Procedures and Structured Programming

TWO DIMENSIONAL ARRAY OR MATRIX common name scores Defined as scores[10][5] IE 10 rows x 5 columns showing name of each position; some values below

Vector: A series of scalars contained in a column or row. Dimensions: How many rows and columns a vector or matrix has.

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Subroutines and Functions

Chapter 9 Introduction to Arrays. Fundamentals of Java

Fundamental of Programming (C)

Chapter 8. Fundamental Characteristics of Subprograms. 1. A subprogram has a single entry point

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Solutions to Chapter 8

9. Subprograms. 9.2 Fundamentals of Subprograms

Module 16: Data Flow Analysis in Presence of Procedure Calls Lecture 32: Iteration. The Lecture Contains: Iteration Space.

CS 2461: Computer Architecture 1

Attendance (2) Performance (3) Oral (5) Total (10) Dated Sign of Subject Teacher

A Brief Introduction to Fortran of 15

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Allocating Storage for 1-Dimensional Arrays

Arrays. Storing Multiple Values

CS 177. Lists and Matrices. Week 8

Weeks 6&7: Procedures and Parameter Passing

Chapter 6. A Brief Introduction to Fortran David A. Padua

CS 140 : Numerical Examples on Shared Memory with Cilk++

Monday, October 24, 2016

AMath 483/583 Lecture 8

Matrix Multiplication

1 Introduction to MATLAB

Program Transformations for the Memory Hierarchy

Wednesday, March 14, 2018

CSE 262 Spring Scott B. Baden. Lecture 4 Data parallel programming

Ch. 7: Control Structures

CS 101, Spring 2016 March 22nd Exam 2

A Crash Course in Compilers for Parallel Computing. Mary Hall Fall, L2: Transforms, Reuse, Locality

Subject: Computer Science

Computers in Engineering. Linear Algebra Michael A. Hawker

Computers in Engineering COMP 208. Representing Vectors. Vector Operations 11/29/2007. Scaling. Adding and Subtracting

Arrays. CS Feb 2008

Structured programming

Lecture 13: Two- Dimensional Arrays

CS475 Parallel Programming

SUBPROGRAMS AND MODULES

Columns A[0] A[0][0] = 20 A[0][1] = 30

1 Introduction to MATLAB

Arrays, Vectors Searching, Sorting

Counting Loop: DO-END DO. DO var = initial-value, final-value, step-size

Data Structures CHAPTER 1 1

CHAPTER 3 ELEMENTS OF COMPUTR PROGAMMING - II

Interfacing With Other Programming Languages Using Cython

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

CS101 Computer Programming and Utilization

Introduction to Computer Science Midterm 3 Fall, Points

Arrays in C. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur. Basic Concept

Data Structures and Algorithms. Dynamic Programming

SHAPE Returns the number of elements in each direction in an integer vector.

1.4 Arrays. A Foundation for Programming. Many Variables of the Same Type. Arrays. !!! any program you might want to write

PROGRAMS. EXCELLENT ACADEMY OF ENGINEERING. Telephone: / NORMAL PROGRAM

Module 28.3: nag mv rotation Rotations. Contents

How to declare an array in C?

CS 221 Lecture. Tuesday, 11 October 2011

Null space basis: mxz. zxz I

Personal SE. Functions & Arrays

INF 212 ANALYSIS OF PROG. LANGS PROCEDURES & FUNCTIONS. Instructors: Kaj Dreef Copyright Instructors.

Loops / Repetition Statements

Basic Structure and Low Level Routines

MPI Programming. Henrik R. Nagel Scientific Computing IT Division

Agenda. Cache-Memory Consistency? (1/2) 7/14/2011. New-School Machine Structures (It s a bit more complicated!)

Goals for This Lecture:

Transcription:

Array Processing { Part II Multi-Dimensional Arrays 1. What is a multi-dimensional array? A multi-dimensional array is simply a table (2-dimensional) or a group of tables. The following is a 2-dimensional table. To nd a student's score, we need (a) his/her PIN { ROW index (b) a program number { COLUMN index. Prog 1 Prog 2 Prog 3 1 100 87 100 2 95 100 90 3. 80. 90. 93. 30 100 95 76

A three-dimensional array can be considered as a pile of two-dimensional tables. For example, all three sections of CS201 can be considered as a three dimensional table consisting of three sub-tables: Sec01 Sec02 Sec03 Prog1 Prog2 Prog3 Prog1 Prog2 Prog3 Prog1 Prog2 Prog3 +-----+-----+-----+ +-----+-----+-----+ +-----+-----+-----+ 1 100 98 87 1 78 100 99 1 86 85 100 2 90 100 96 2 100 60 87 2 96 82 100 3 86 100 78 3 65 76 99 3 100 100 95 : : : : : : : : : : : : 30 100 86 96 30 88 98 100 30 79 94 97 +-----+-----+-----+ +-----+-----+-----+ +-----+-----+-----+ Thus, to nd a student's score, we need (a) a section number { TABLE index (b) his/her PIN { ROW index (c) a program number { COLUMN index. So, THREE indices are required.

2. What are the important components of a multi-dimensional array? Its type:, REAL, LOGICAL and CHARACTER Its extents: Since a multi-dimensional array has more than one indices, more than one extents are required. Indices (or subscripts) must be integers. The smallest and the largest indices (or subscripts) are referred to as the lower bound and upper bound of that extent. 3. How to declare an array? It is exactly identical to declaring one-dimensioanl arrays. But, thre are more extents: REAL, DIMENSION(-5:5, 10:15) REAL, DIEMNSION(100:200, -1:1) REAL, DIMENSION(1:10, 1:10) :: x :: Sum The above declares three arrays: :: Value (a) Array x is a two-dimensional array. The rst (resp., second) extent has lower bound and upper bound -5 and 5 (resp., 10 and 15). (b) Array Sum is a two-dimensional array. The rst (resp., second) extent haslower bound and upper bound 100 and 200 (resp., -1 and 1).

(c) Array Value is a two-dimensional array. The rst (resp., second) extent has lower bound and upper bound 1 and 10 (resp., 1 and 10). If the lower bound is 1, the lower bound value and the colon can be removed. 4. What are the array elements? REAL, DIMENSION(-5:5, 10:15) :: x The rst index gives the ROW number, while the second provides the COLUMN number. Thus, the -5th rowofarray x() is x(-5,10), x(-5,11), x(-5,12), x(-5,13), x(-5,14) and x(-5,15). The 0th row of x() has elements x(0,10), x(0,11), x(0,12), x(0,13), x(0,14) and x(0,15). The 4th row of x() has elements x(4,10), x(4,11), x(4,12), x(4,13), x(4,14) and x(4,15).

Array Input and Output You can read in array elements Row-by-Row, one on each line, PARAMETER :: MAXROW = 3, MAXCOL = 5, DIMENSION(1:MAXROW,1:MAXCOL) :: X ROW, COL IF (ROW < 1.OR. ROW > MAXROW) THEN 'Bad row number' ELSE IF (COL < 1.OR. COL > MAXCOL) THEN ELSE DO I = 1, ROW DO J = 1, COL END IF The input is: 'Bad column number' X(I,J) :: ROW, COL, I, J 2 3 ROW = 2, COL = 3 4 Col 1 Col 2 Col 3 Col 4 Col 5 8 +-----+-----+-----+-----+-----+ 3 Row 1 4 8 3?? 9 Row 2 9 7 5?? 7 Row 3????? 5 +-----+-----+-----+-----+-----+

You can also read in array elements Column-by-Column, one on each line, PARAMETER :: MAXROW = 3, MAXCOL = 5, DIMENSION(1:MAXROW,1:MAXCOL) :: X :: ROW, COL, I, J ROW, COL IF (ROW < 1.OR. ROW > MAXROW) THEN 'Bad row number' ELSE IF (COL < 1.OR. COL > MAXCOL) THEN ELSE END IF 'Bad column number' DO J = 1, COL <------------------ DO I = 1, ROW <--------------- X(I,J) changed changed The input is the same, but the array content is VERY dierent. 2 3 ROW = 2, COL = 3 4 Col 1 Col 2 Col 3 Col 4 Col 5 8 +-----+-----+-----+-----+-----+ 3 Row 1 4 3 7?? 9 Row 2 8 9 5?? 7 Row 3????? 5 +-----+-----+-----+-----+-----+

Or, you can read in array elements Row-by-Row with an implied DO, PARAMETER :: MAXROW = 3, MAXCOL = 5, DIMENSION(1:MAXROW,1:MAXCOL) :: X :: ROW, COL, I, J ROW, COL IF (ROW < 1.OR. ROW > MAXROW) THEN 'Bad row number' ELSE IF (COL < 1.OR. COL > MAXCOL) THEN ELSE DO I = 1, ROW END IF 'Bad column number' (X(I, J), J = 1, COL) <------ changed Now you have to organize the input row-by-row! 2 3 ROW = 2, COL = 3 4 8 3 Col 1 Col 2 Col 3 Col 4 Col 5 9 7 5 +-----+-----+-----+-----+-----+ Row 1 4 8 3?? Row 2 9 7 5?? Row 3????? +-----+-----+-----+-----+-----+

Or, you can read array elements in Column-by-Column with an implied DO, PARAMETER = MAXROW = 3, MAXCOL = 5, DIMENSION(1:MAXROW,1:MAXCOL) :: X :: ROW, COL, I, J ROW, COL IF (ROW < 1.OR. ROW > MAXROW) THEN 'Bad row number' ELSE IF (COL < 1.OR. COL > MAXCOL) THEN ELSE 'Bad column number' DO J = 1, COL <------------------------- END IF changed (X(I, J), I = 1, ROW) <------ changed You have to organize the input column-by-column! 2 3 ROW = 2, COL = 3 4 9 Col 1 Col 2 Col 3 Col 4 Col 5 8 7 +-----+-----+-----+-----+-----+ 3 5 Row 1 4 8 3?? Row 2 9 7 5?? Row 3????? +-----+-----+-----+-----+-----+

The following reads in the table column-by-column, PARAMETER :: MAXROW = 3, MAXCOL = 5, DIMENSION(1:MAXROW,1:MAXCOL) :: X ROW, COL IF (ROW < 1.OR. ROW > MAXROW) THEN 'Bad row number' ELSE IF (COL < 1.OR. COL > MAXCOL) THEN 'Bad column number' ELSE DO J = 1, COL <------------------------- :: ROW, COL, I, J changed (X(I, J), I = 1, ROW) <------ changed END IF What if you DID NOT change your input accordingly? The following input is organized row-by-row: 2 3 ROW = 2, COL = 3 4 8 3 Col 1 Col 2 Col 3 Col 4 Col 5 9 7 5 +-----+-----+-----+-----+-----+ Row 1 4 9??? Row 2 8 7??? Row 3????? +-----+-----+-----+-----+-----+ 3 on the rst line and 5 on the second are IGNORED, and there is no more data for the third column. OOPS!

Using Implied DO Loop to Read a Table Syntax 1. The following is equivalent to REAL, DIMENSION(1:3,1:5) :: A ((A(I,J), J = 1, 5), I = 1, 3) The outer implied DO expands to (A(1,J), =1,5), (A(2,J),j=1,5), (A(3,J),j=1,5) Expanding all three implied DOs yields: A(1,1), A(1,2), A(1,3), A(1,4), A(1,5), & A(2,1), A(2,2), A(2,3), A(2,4), A(2,5), & A(3,1), A(3,2), A(3,3), A(3,4), A(3,5) 2. This is a row-by-row read:, PARAMETER :: MAXROW = 3, MAXCOL = 5, DIMENSION(1:MAXROW,1:MAXCOL) :: X :: ROW, COL, I, J ROW, COL ((X(I,J), J = 1, COL), I = 1, ROW) 2 3 ROW = 2, COL = 3 4 8 3 Col 1 Col 2 Col 3 Col 4 Col 5 9 7 5 +-----+-----+-----+-----+-----+ Row 1 4 8 3?? Row 2 9 7 5?? Row 3????? +-----+-----+-----+-----+-----+

3. This is a column-by-column read:, PARAMETER :: MAXROW = 3, MAXCOL = 5, DIMENSION(1:MAXROW,1:MAXCOL) :: X :: ROW, COL, I, J ROW, COL ((X(I,J), I = 1, ROW), J = 1, COL) 2 3 ROW = 2, COL = 3 4 9 Col 1 Col 2 Col 3 Col 4 Col 5 8 7 +-----+-----+-----+-----+-----+ 3 5 Row 1 4 8 3?? Row 2 9 7 5?? Row 3????? +-----+-----+-----+-----+-----+

Using Arrays in Computation 1. Clear an array to zero:, PARAMETER :: MAXROW = 20, MAXCOL = 100, DIMENSION(1:MAXROW,1:MAXCOL) :: A DO i = 1, MAXROW DO j = 1, MAXCOL A(i,j) = 0 2. Even rows have 1 and odd rows have 0, PARAMETER :: BOUND = 20, DIMENSION(1:BOUND,1:BOUND) :: A DO i = 1, BOUND IF (MOD(i,2) == 0) THEN DO j = 1, BOUND A(i,j) = 1 ELSE DO j = 1, BOUND A(i,j) = 0 END IF

3. Diagonal elements A(1,1), A(2,2), A(3,3),... receive 1 while others receive zero. (a) Method 1, PARAMETER :: BOUND = 20, DIMENSION(1:BOUND,1:BOUND) :: A DO i = 1, BOUND DO j = 1, BOUND IF (i == j) THEN ELSE A(i,i) = 1 A(i,j) = 0 END IF (b) Method 2, PARAMETER :: BOUND = 20, DIMENSION(1:BOUND,1:BOUND) :: A DO i = 1, BOUND DO j = 1, BOUND A(i,j) = 0 A(i,i) = 1

4. Compute the sum of all array elements:, PARAMETER :: BOUND = 20, DIMENSION(1:BOUND,1:BOUND) :: A Sum = 0 DO i = 1, BOUND DO j = 1, BOUND Sum = Sum + A(i,j) :: Sum, i, j 5. Compute the sum of the corresponding elements of two arrays into a third one:, PARAMETER :: BOUND = 20, DIMENSION(1:BOUND,1:BOUND) :: A, B, C DO i = 1, BOUND DO j = 1, BOUND C(i,j) = A(i,j) + B(i,j)

6. Clear the boarder of a table to 1 and interior elements to zero., PARAMETER :: BOUND = 20, DIMENSION(1:BOUND,1:BOUND) :: A DO i = 2, BOUND-1 <---+ DO j = 2, BOUND-1 A(i,j) = 0 A(i,1) = 1 A(i,BOUND) = 1 <-----------------+ DO j = 1, BOUND A(1,j) = 1 A(BOUND,j) = 1 7. Transpose a square table., PARAMETER :: BOUND = 20, DIMENSION(BOUND,BOUND) :: A INGETER DO i = 1, BOUND-1 +- row 2 to row BOUND-1, Temp DO j = i+1, BOUND 1 2 3 4 -> 1 5 9 13 Temp = A(i,j) 5 6 7 8 2 6 10 14 A(i,j) = A(j,i) 9 10 11 12 3 7 11 15 A(j,i) = Temp 13 14 15 16 4 8 12 16

Multi-Dimensional Arrays as Arguments 1. Multi-dimensional arrays can be passed to FUNCTIONSs and SUBROUTINEs. The corresponding formal arguments must be declared as arrays with the same number of extents and structure: PROGRAM IMPLICIT Example NONE, PARAMETER :: L1 = 1, U1 = 10, L2 = -5, U2 = 5, DIMENSION(L1:U1,L2:U2) :: x CALL Sub(x, L1, U1, L2, U2) CONTAINS SUBROUTINE Sub(a, Lo1, Up1, Lo2, Up2) IMPLICIT NONE, DIMENSION(Lo1:Up1,Lo2:Up2), INTENT(IN) :: a, INTENT(IN), INTENT(IN) END SUBROUTINE END PROGRAM Example Sub :: Lo1, Up1 :: Lo2, Up2

2. Or, you may want to use assumed-shape arrays. In this case, only lower bounds are required. PROGRAM IMPLICIT Example NONE, PARAMETER :: L1 = 1, U1 = 10, L2 = -5, U2 = 5, DIMENSION(L1:U1,L2:U2) :: x CALL Sub(x, L1, L2) CONTAINS SUBROUTINE Sub(a, Lo1, Lo2) IMPLICIT NONE, DIMENSION(Lo1:,Lo2:), INTENT(IN) :: a, INTENT(IN), INTENT(IN) END SUBROUTINE END PROGRAM Example Sub :: Lo1 :: Lo2

3. As a reminder, if a lower bound is 1, you do not have to pass it. PROGRAM IMPLICIT Example NONE, PARAMETER :: L1 = 1, U1 = 10, L2 = -5, U2 = 5, DIMENSION(L1:U1,L2:U2) :: x CALL Sub(x, L2) CONTAINS SUBROUTINE Sub(a, Lo2) IMPLICIT NONE, DIMENSION(1:,Lo2:), INTENT(IN) :: a, INTENT(IN) END SUBROUTINE END PROGRAM Example Sub :: Lo2

Example 1: Table and Their Row/Column Sums PROGRAM IMPLICIT TableSums NONE, PARAMETER :: ROW_SIZE = 5, COLUMN_SIZE = 6, DIMENSION(1:ROW_SIZE,1:COLUMN_SIZE) :: Table, DIMENSION(1:ROW_SIZE) :: RowSum, DIMENSION(1:COLUMN_SIZE) :: ColumnSum :: RowSize, ColumnSize CALL CALL CALL CALL ReadTable(Table, RowSize, ColumnSize) ComputeRowSum(Table, RowSize, ColumnSize, RowSum) ComputeColumnSum(Table, RowSize, ColumnSize, ColumnSum) DisplayTable(Table, RowSize, ColumnSize, RowSum, ColumnSum) CALL CALL ComputeSums(Table, RowSize, ColumnSize, RowSum, ColumnSum) DisplayTable(Table, RowSize, ColumnSize, RowSum, ColumnSum) CONTAINS SUBROUTINE ReadTable(Table, Rows, Columns) IMPLICIT NONE, DIMENSION(1:,1:), INTENT(OUT) :: Table, INTENT(OUT) :: Rows, Columns Rows, Columns DO i = 1, Rows (Table(i,j), j=1, Columns) END SUBROUTINE ReadTable

SUBROUTINE IMPLICIT DisplayTable(Table, Rows, Columns, RowSum, ColumnSum) NONE, DIMENSION(1:,1:), INTENT(IN) :: Table, DIMENSION(1:), INTENT(OUT) :: RowSum, ColumnSum, INTENT(IN) :: Rows, Columns :: Total Total = 0 DO i = 1, Rows "The input table and row/column sums:" (Table(i,j), j=1, Columns), RowSum(i) Total = Total + RowSum(i) END SUBROUTINE (ColumnSum(j), j=1, Columns), Total DisplayTable SUBROUTINE ComputeRowSum(Table, Rows, Columns, RowSum) IMPLICIT NONE, DIMENSION(1:,1:), INTENT(IN) :: Table, DIMENSION(1:), INTENT(OUT) :: RowSum, INTENT(IN) :: Rows, Columns DO i = 1, Rows RowSum(i) = 0 DO j = 1, Columns RowSum(i) = RowSum(i) + Table(i,j) END SUBROUTINE ComputeRowSum

SUBROUTINE IMPLICIT ComputeColumnSum(Table, Rows, Columns, ColumnSum) NONE, DIMENSION(1:,1:), INTENT(IN) :: Table, DIMENSION(1:), INTENT(OUT) :: ColumnSum, INTENT(IN) :: Rows, Columns DO j = 1, Columns ColumnSum(j) = 0 DO i = 1, Rows ColumnSum(j) = ColumnSum(j) + Table(i,j) END SUBROUTINE ComputeColumnSum SUBROUTINE IMPLICIT ComputeSums(Table, Rows, Columns, RowSum, ColumnSum) NONE, DIMENSION(1:,1:), INTENT(IN) :: Table, DIMENSION(1:), INTENT(OUT) :: RowSum, ColumnSum, INTENT(IN) :: Rows, Columns DO j = 1, Columns ColumnSum(j) = 0 DO i = 1, Rows RowSum(i) = 0 DO j = 1, Columns RowSum(i) = RowSum(i) + Table(i,j) ColumnSum(j) = ColumnSum(j) + Table(i,j) END SUBROUTINE ComputeSums END PROGRAM TableSums

Example 2: Matrix Multiplication Given matrix A = [aik] of ` rows and m columns and matrix B =[bkj] ofmrows and n colums, their product C =[cij] is computed as follows: X cij = m aikbkj k=1 = ai1b 1j + ai2b 2j + + aimbmj where matrix C has ` rows and n columns. The following is an example: 2 64 1 3 2 4 5 7 3 75 2 64 1 3 5 7 2 4 6 8 3 75 = 2 64 7 15 23 31 10 22 34 46 19 43 67 91 where 34 on the row 2 and column 3 is computed as follows: 34 = 2 5+4 6 Note that [2 4] is row #2 of matrix A and [5 6] is column #3 of matrix B. The number of columns of A and the number of rows of B must be equal in order to compute A B 3 75

PROGRAM IMPLICIT MatrixMultiplication NONE, PARAMETER :: MAX_SIZE = 20, DIMENSION(1:MAX_SIZE,1:MAX_SIZE) :: X, Y, Z :: RowX, ColumnX :: RowY, ColumnY :: RowZ, ColumnZ CALL ReadMatrix(X, RowX, ColumnX) "Input Matrix A:" CALL DisplayMatrix(X, RowX, ColumnX) CALL ReadMatrix(Y, RowY, ColumnY) "Input Matrix B:" CALL DisplayMatrix(Y, RowY, ColumnY) IF (ColumnX /= RowY) THEN ELSE RowZ = RowX "ERROR: Cannot multiply" ColumnZ = ColumnY CALL Multiply(X, RowX, ColumnX, Y, RowY, ColumnY, Z) "Matrix A*B:" CALL DisplayMatrix(Z, RowZ, ColumnZ) END IF CONTAINS SUBROUTINE IMPLICIT ReadMatrix(A, Row, Column) NONE, DIMENSION(1:,1:), INTENT(OUT) :: A, INTENT(OUT) :: Row, Column DO i = 1, Row END SUBROUTINE Row, Column (A(i,j), j = 1, Column) ReadMatrix

SUBROUTINE IMPLICIT DisplayMatrix(A, Row, Column) NONE, DIMENSION(1:,1:), INTENT(IN) :: A, INTENT(IN) :: Row, Column DO i = 1, Row END SUBROUTINE (A(i,j), j = 1, Column) DisplayMatrix SUBROUTINE Multiply(A, RowA, ColumnA, B, RowB, ColumnB, C) IMPLICIT NONE, DIMENSION(1:,1:), INTENT(IN) :: A, B, DIMENSION(1:,1:), INTENT(OUT) :: C, INTENT(IN) :: RowA, ColumnA, INTENT(IN) :: RowB, ColumnB :: Sum, k DO i = 1, RowA DO j = 1, ColumnB Sum = 0 END SUBROUTINE END PROGRAM DO k = 1, ColumnA Sum = Sum + A(i,k)*B(k,j) C(i,j) = SUm Multiply MatrixMultiplication

Ordering of Array Elements Although FORTRAN supports two-dimensional arrays, IN MEMORY a two dimensional array is always organized COLUMNWISE., DIMENSION(1:3,1:4) :: X +--------+--------+--------+--------+ +--------+ X(1,1) X(1,2) X(1,3) X(1,4) X(1,1) + <--+ +--------+--------+--------+--------+ +--------+ X(2,1) X(2,2) X(2,3) X(2,4) X(2,1) +-- Col. 1 +--------+--------+--------+--------+ +--------+ X(3,1) X(3,2) X(3,3) X(3,4) X(3,1) <--+ +--------+--------+--------+--------+ +--------+ X(1,2) <--+ +--------+ X(2,2) +-- Col. 2 +--------+ X(3,2) <--+ +--------+ X(1,3) <--+ +--------+ X(2,3) +-- Col. 3 +--------+ X(3,3) <--+ +--------+ X(1,4) <--+ +--------+ X(2,4) +-- Col. 4 +--------+ X(3,4) <--+ +--------+