Multi-dimensional Array in Data Structure

Rumman Ansari   Software Engineer   2023-03-27   28434 Share
☰ Table of Contents

Table of Content:


Note: Before going to the array in data structure please read the basic array concept from our c programming tutorial here is the link:

Single Dimensional array C Programming Language

Multi Dimensional array in C Programming Language

Mult-idimensional arrays are defined analogously. More specifically, an n dimension m1 x m2 ... x mn array B is a collection of m1, m2 , ..., mn data elements in which each element specified by a list of n integers such as K1, K2....., Kn called subscripts with the property that

1<=K1<=m. 1<=k2<=m2...........1<=Kn<=mn

The array will be stored in memory in a sequence of memory locations. Specifically,

The programming language will store the array B, either in row major order or in column major order by row major order, we mean that the elements are listed so that the subscribs vary like an automobile audomeetre, i.e., so that the last subscripts varies first (most rapidly) the next to last subscript varies second (less rapidly) and so on. By column major order we mean that the elements are listed so that the first subscript varies first (most rapidly), the second subscript second (less. rapidly) and so on.

For a 2 x 4 x 3 array B. The number of elements in B =2 • 4 •3 =24. These 24 elements of B are usually pictured as in given Fig. 4. below, i.e., they appear in three layers, called pages, where each page consists of the 2 x 4 rectangular array of elements with the same third subscript. The tree subscript of an element in a 3-dimensional array and called respectively row, column, page.

Column Major Order (RMO)

Column Major Order

Column Major Order (RMO)

Row Major Order

Row Major Order (RMO)

The definition of general multi-dimensional array also permits lower bound other than 1. Let, C be such an n-dimensional array. As before, the index set for each dimension of C consists of the consecutive integers from the lower bound to the upper bound of the dimension. The length Li of dimension i of C is the number of elements in an index set and Li can be calculated, as before from

Li = UB - LB +1

For a given subsctript Ki, the effective index Ei of Li is the number of indices preceding Ki in the index set,

Ei = Ki - Lower Bound

Then, the address LOC (C[K1.K2....KN] of an arbitrary element of C can be obtained from the formulae.

Base (C) + w[(((.....(ENLN-1+EN-1)LN-2)+....+E3L2+E2)L1+E1]

Or from the formulae,

Base (C) + w[(.....(( (E1L2+E2)L3+E3)L4+....+EN-1)[N+EN]

According to whether C is stored in column n major or row major order. Once again, Base (C) denotes the address of the first element of C and w denotes the number of words per memory locations.

Example:

Suppose multi-dimensional arrays A and B are declared using

A (-2 : 2, 2 : 22) and B (1 : B, -5 : 5, - 10 : 5)

  • Find the length of each dimension and the number of elements in A and B.
  • Considers the element B[3, 3, 3] in B. Find the effective indices E1, E2, E3 and the address of the element, assuming Base (B) = 400 and there are w = 4-word memory location.

(a) The length of a dimension is obtained by

Length = Upper Bound - Lower bound + 1

Hence, the lengths 4 of the dimension of A are,

L1=2-(-2)+ 1=5

L2=22-2+1=21

Accordingly A has 21.5 = 105 elements. The lengths Lof the dimension of B are,

L1=8-1+1=8

L2=5-(-5)+ 1=11

L3=5-(-10)+1=16

Therefore, B has 8.11.16 = 1408 elements.

 

(b) The effective index Ei is obtained from Ei=ki- LB, where ki is the given index and LB, is the lower bound. Hence,

E1= 3 - 1= 2

E2 = 3 - (-5) = 8

E3 =3 -(-10)= 13

The address depends on whether the programming language stores B in row-major order or column major order. Assuming B is stored in column major order.

E3L2 =13.11=143

E3L2 + E2 =143 + 8 = 151

(E3L2)L1 = 151.8 = 1208

(E2L2+E2)L1+E1 = 1208 + 2 =1210

 

Therefore, LOC(B[3,3,3]) = 400+4(1210) = 400 +4840 = 5240