Courses βeta
Data Structures

## Multi-Dimensional Arrays

A multi-dimensional array of dimension n (i.e., an n-dimensional array or simply n-D array) is a collection of items which is accessed via n subscript expressions.

Initialization of Multidimensional Arrays

// In C, multidimensional arrays can be initialized in the following ways:

int x[2][3];
OR
int x[2][3] = {{1, 3, 0}, {-1, 5, 9}};
OR
int x[][3] = {{1, 3, 0}, {-1, 5, 9}};
OR
int x[2][3] = {1, 3, 0, -1, 5, 9};


// In Java, multidimensional arrays can be initialized in the following ways:

int[][] A = new int[3][4];
OR
int[][]  A  =  {  {  1,  0, 12, -1 },
{  7, -3,  2,  5 },
{ -5, -2,  2, -9 }
};


#In Python, multidimensional arrays can be initialized in the following ways:

multi_array = [
[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10]
]

#Extracting data:
multi_array[0][0]
#Output: 1


Suppose there is a multidimensional array arr[ i ][ j ][ k ][ m ]. Then this array can hold i * j * k * m numbers of data elements.

Row Major order:

In row-major order, we traverse the matrix by row by row, then within each row enumerates the column.

Matrix A = a11 a12 a13
a21 a22 a23

So, the matrix A, if stored in row-major order, would be stored in memory linearly as a11, a12, a13, a21, a22, a23.

Column Major order:

In column-major order, we traverse the matrix by column by column, then enumerates the rows within each column.

Matrix A = a11 a12 a13
a21 a22 a23

So, the matrix A, if stored in column-major order, would be stored in memory linearly as a11, a21, a12, a22, a13, a23.

While storing the elements of a 2-D array in memory, these are allocated contiguous memory locations. Therefore, a 2-D array must be linearized so as to enable their storage. There are two alternatives to achieve linearization: Row-Major and Column-Major.

Address of an element of any array say “A[ I ][ J ]” is calculated in two forms as given: (1) Row Major System (2) Column Major System

Row Major System:
The address of a location in Row Major System is calculated using the following formula:

Address of A[I][J] =
B + W * [ N * ( I – Lr ) + ( J – Lc ) ] 
Column Major System:
The address of a location in Column Major System is calculated using the following formula:
Address of A[I][J] =
B + W * [( I – Lr ) + M * ( J – Lc )]  
Where,
I = Row subscript of element whose address is to be found
J = Column subscript of element whose address is to be found
W = Storage Size of one element stored in the array (in byte)
Lr = Lower limit of row/start row index of matrix, if not given assume 0 (zero)
Lc = Lower limit of column/start column index of matrix, if not given assume 0 (zero)
M = Number of row of the given matrix
N = Number of column of the given matrix

Note :
Usually number of rows and columns of a matrix are given ( like A[20][30] or A[40][60] ) but if it is given as A[Lr- – – – – Ur, Lc- – – – – Uc]. In this case number of rows and columns are calculated using the following methods:

Number of rows (M) will be calculated as = (Ur – Lr) + 1 Number of columns (N) will be calculated as = (Uc – Lc) + 1

For Example:

An array X [-15……….10, 15……………40] requires one byte of storage. If beginning location is 1500 determine the location of X [15][20].

Solution:
As you see here the number of rows and columns are not given in the question. So they are calculated as:
Number or rows say M = (Ur – Lr) + 1 = [10 – (- 15)] +1 = 26
Number or columns say N = (Uc – Lc) + 1 = [40 – 15)] +1 = 26

(i) Column Major Wise Calculation of above equation
The given values are: B = 1500, W = 1 byte, I = 15, J = 20, Lr = -15, Lc = 15, M = 26
Address of A [ I ][ J ] = B + W * [ ( I – Lr ) + M * ( J – Lc ) ]
= 1500 + 1 * [(15 – (-15)) + 26 * (20 – 15)] = 1500 + 1 * [30 + 26 * 5] = 1500 + 1 * [160] = 1660 [Ans]

(ii) Row Major Wise Calculation of above equation
The given values are: B = 1500, W = 1 byte, I = 15, J = 20, Lr = -15, Lc = 15, N = 26
Address of A [ I ][ J ] = B + W * [ N * ( I – Lr ) + ( J – Lc ) ]
= 1500 + 1* [26 * (15 – (-15))) + (20 – 15)] = 1500 + 1 * [26 * 30 + 5] = 1500 + 1 * [780 + 5] = 1500 + 785
= 2285 [Ans]