Professional Documents
Culture Documents
Two - Dimensional arrays : A 2- dimensional MxN array A is a collection of M.N data elements
such that each element is specified by a pair of integers (J,K) called subscripts with the property
that 1<= J <=M and 1<= K <= N.
The element of A with first subscript J and second subscript K will be denoted by AJ,K or A[J,K].
Two-dimensional arrays are called matrices in mathematics and tables in business applications.
Rectangular Representation :
Two dimensional 3x4 array A = A[1,1] A[1,2] A[1,3] A[1,4]
A[2,1] A[2,2] A[2,3] A[2,4]
A[3,1] A[3,2] A[3,3] A[3,4]
A[4,1] A[4,2] A[4,3] A[4,4]
Representation in Memory:
The array will be represented in memory by a block of MxN sequential location.
Storage Schemes for Multi-Dimensional Arrays
Since main memory is linear, multi-dimensional arrays are stored in a linear fashion in
computers' memory, rather than in rectangular fashion as we see them on paper. Programming
languages use one of the following storage schemes for multi-dimensional arrays
1. Row-Major (row by row)
2. Column-Major (column by column)
Row-Major Storage
Starting at the base address i.e., the starting address of array (address of first element in the
array), row major ordering assigns successive elements from row 0 to successive memory
locations, then successive elements from row 1 and so on. As an example, the row-major
storage of a 2D matrix A[4][4] is shown below.
A 12 -45 20 14
15 -87 0 13
12 -54 30 40
50 60 70 80
Addres 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
s
Row major ordering is the method employed by most high level programming languages
including C/C++, Java, Ada, etc. It is very easy to implement and easy to use in machine
language. The concept is easily extensible to higher dimensional arrays. The only thing to be
remembered is that the rightmost dimension increases the fastest, then the one next to the
rightmost, and so on till the leftmost dimension that increases the slowest.
A 12 -45 20 14
15 -87 0 13
12 -54 30 40
50 60 70 80
Addres 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
s
MATRIX MULTIPLICATION
Algorithm ( Matrix Multiplication ) MATMUL (A,B,C,M,P,N)
Let A be an MxP matrix array and let B be a PxN matrix array. This algorithm stores the product
of A and B in an MxN matrix array C.