You are on page 1of 3

Topic : Multidimensional Arrays

Second Year Telecommunication Engineering


Batch 2016-17

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

Content 12 -45 20 14 15 -87 0 13 12 -54 30 40 50 60 70 80


s

Row-Major Storage of Matrix A


Topic : Multidimensional Arrays
Second Year Telecommunication Engineering
Batch 2016-17

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.

The address of A[J,K] is computed as


LOC(A[J,K])= Base (A) + W[N(J-1)+ (K-1)] ; W= no of bytes of one element
For Example: LOC (A[2,3]) = 00+ 1[4(2-1) +(3-1)]
=00+6 =06
Column-Major Storage
Starting at the base address, the column major ordering assigns successive elements from
column 0 to successive memory locations, then successive elements from column 1 and so on.
FORTRAN and MATLAB store matrices in column-major order. Continuing the same example,
column-major storage of matrix A 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

Content 12 15 12 50 -45 -87 -54 60 20 0 30 70 14 13 40 80


s

Column-Major Storage of Matrix A

The address of A[J,K] is computed as


LOC(A[J,K])= Base (A) + W[(J-1)+M(K-1)] ; W= no of bytes of one element
For Example LOC( A[2,3] )= 00 +1[(2-1) +4(3-1)]
=00+1[1+8] = 00+9 = 09
Topic : Multidimensional Arrays
Second Year Telecommunication Engineering
Batch 2016-17

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.

1. Repeat step 2 to 4 for I= 1 to M


2. Repeat steps 3 and 4 for J = 1 to N
3. Set C[I,J] :=0 [Initializes C[I,J]]
4. Repeat for K =1 to P
5. C [I,J] := C[I,J] + A[I,K] *B[K,J]
[end of inner loop ]
[end of step 2 middle loop]
[end of step 1 outer loop]

You might also like