You are on page 1of 9

Multidimensional Arrays

Two-Dimensional Arrays
 A two-dimensional m ᵡ n array A is a
collection of m.n data elements such that
each element is specified by a pair of
integers, called subscripts, with the property
that
1≤J≤m & 1≤K≤n

It is also called matrices/matrix arrays in mathematics,


tables in business applications.
Example
 Suppose each student in a class of 25 students is given 4 tests.
Assuming the students are numbered from 1 to 25, the test scores
can be assigned to a 25 ᵡ 4 matrix array SCORE as in table. Thus
SCORE[K,L] contains the Kth student’s score on the Lth test. In
particular, the second row of the array,
SCORE[2,1], SCORE[2,2], SCORE[2,3], SCORE[2,4]
Contains the four test scores of the second student.

Student Test 1 Test 2 Test 3 Test 4


1 84 73 78 88
2 56 58 78 67
3 56 77 45 67
: : : : :
25 76 78 80 45
Formula to calculate the Two-D
Arrays
 Length = UB – LB +1
 eg: - If INTEGER NUM (2:5, -3:1), then find the
length of 1st & 2nd dimensions.
 Sol:- Length of 1st dimension = 5 – 2 + 1 = 4
Length of 1st dimension = 1 – (-3) + 1 = 5
Number contains 4 * 5 = 20 elements.
Representation of Two-D Arrays
 Divided into two parts: -
 Column-major order (Column by Column)
 Row-major order (Row by Row)
A RC B RC
(1,1) (1,1)
(2,1) (1,2)
(3,1) (1,3)
(1,2) (2,1)
(2,2) (2,2)
(3,2) (2,3)
Computing of 2-D Arrays
 Column-major order:-
 LOC(A[J,K]) = Base(A) + w[M(K – 1) + (J – 1)]

 Row-major order: -
 LOC(A[J,K]) = Base(A) + w[N(J – 1) + (K – 1)]

 eg:- Consider the 25 * 4 matrix array SCORE in previous example.


Suppose Base(SCORE) = 200 & there are w = 4 words per memory
cell. Furthermore, suppose the PL stores 2-D arrays using row-major
order. Then the address of SCORE[12,3], the third test of the 12 th
student, follows:

 LOC(SCORE[12,3]) = 200 + 4 [4(12 – 1) + (3 – 1)] = 200 + 4[46] = 384


Multidimensional Arrays
 Li = UB – LB + 1
for a given subscript Ki, the effective index Ei of Li is
the number of indices preceding Ki in the index set, &
Ei can be calculated from
Ei = Ki – LB
Base (C) + w[(…((E1L2 + E2)L3 + E3)L4 +….+EN-1 )LN + EN
How to Calculate
Multidimensional Arrays
 Suppose a 3-D array MAZE is declared using MAZE(2:8, -4:1, 6:10)
Then the lengths of the 3-D of the MAZE are, respectively,
L1 = 8 – 2 + 1 = 7, L2 = 1 – (-4) + 1 = 6, L3 = 10 – 6 + 1 = 5
Accordingly, MAZE contains L1 . L2 . L3 = 7 * 6 * 5 = 210 elements
suppose the PL stores MAZE in memory in row-major order &
suppose Base(MAZE) = 200 & there are w = 4 words per memory
cell.
eg: - The address of MAZE[5, -1, 8]:-
E1 = 5 – 2 = 3 E2 = -1 – (-4) = 3 E3 = 8 – 6 = 2
E1 L2 = 3 * 6 = 18
E1 L2 + E2 = 18 + 6 = 21
(E1 L2 + E2)L3 = 21 * 5 = 105
(E1 L2 + E2)L3 + E3 = 105 + 2 = 107
Therefore, LOC(MAZE[5, -1, 8] = 200 + 4(107) = 628
Thank You!!!

You might also like