Multidimensional array

In this module, we will cover
  • How multidimensional arrays are declared?
  • How two-dimensional arrays are initialized, accessed, and modified?
  • Memory allocation - row-major order and column-major order
A multidimensional array of dimension n is a collection of similar items where each item is an array of dimension n-1
Ex: The four-dimensional array is a collection of three-dimensional arrays

Declaration of a multidimensional array

Syntax 

data_type array_name[dim1][dim2]...[dimN];
where dim1, dim2, ... , dimN are the dimensions of the array

Example

int X[5][4][6][2];

Two-dimensional array: a special case of the multidimensional array

From now onward we will understand multidimensional array with the help of two-dimensional array which is the simplest form of the multidimensional array having two dimensions. The first dimension refers to the number of rows and the second dimension refers to the number of columns. 2 D arrays are processed using two loops.

Two-dimensional arrays are also referred to as the matrix.

Syntax

data_type array_name[row][col];

Example

int X[5][7];



Memory allocation of the two-dimensional array
2 D arrays are processed in one of the two ways:

Row major order : 

first, all the elements of row 1 are processed, then all the elements of row 2, and so on.

If an array has m rows and n columns,
address of X[i][j] = base_address + element_size * [( i - 0 )*n + (j - 0)]

Note: I will expect you to visualize rather than memorizing the formula  

Let's consider a 2 D array, 
int X[5][7];
the base address of X is 1000, what is the address of X[3][4] ? ( first try on your own before scrolling down )



address of X[0][0] = 1000 , then address of X[3][0] = 1000 + 4 * (3*7) = 1084
now address of X[3][4] = address of X[3][0] + 4 * (4) = 1084 + 16 = 1100

Column major order :

first all the elements of column 1 are processed, then all the elements of column 2, and so on.


If an array has m rows and n columns,
address of X[i][j] = base_address + element_size * [(j - 0)*m + (i - 0)]


Note: I will expect you to visualize rather than memorizing the formula  

Let's consider a 2 D array, 
int X[5][7];
the base address of X is 1000, what is the address of X[3][4]? ( first, try on your own before scrolling down )



address of X[0][0] = 1000 , then address of X[0][4] = 1000 + 4 * (4*5) = 1080
now address of X[3][4] = address of X[0][4] + 4 * (3) = 1080 + 12 = 1092

Initialisation of two dimensional array

2 D arrays are initialized in any of the two ways discussed below :

int X[3][4] = { 1, 3, 5, 7, 9, 11, 32, 45, 4, 55, 13, 21 };
In this case X[0][0] = 1, X[0][1] = 3, ... , X[2][3] = 21




It is not necessary to initialize all the elements,
you would have written
int X[3][4] = { 1, 3, 5, 7, 9, 11, 32, 45 }; 
In that case X[0][0] = 1, X[0][1] = 3, ... ,
X[1][3] = 45 rest all are assigned to zero


Another way of initialization is
int X[3][4] = { { 1, 3, 5 }, {7, 9, 11, 32 }, { 45 } };


Arrays can be accessed and modified with the help of row number and column number
Assuming row and column are numbered from zero,
If we want to know the element at row 1 and column 2 of array X, then X[1][2] will give us the required element

I want to create a 3 x 4 array, where each element is equal to the sum of row number and column number

int arr[3][4];
for( int i = 0; i < 3; i++ )
     for( int j = 0; j < 4; j++ )
          arr[i][j] = i + j;


Now, let's say I want to modify all the elements where row number is less than the column number

forint i = 0; i < 3; i++ )
     forint j = 0; j < 4; j++ )
          if( i < j)
              arr[i][j] = 0;



Practice Problems

  1. Roy and Symmetric Logos ( HackerEarth )
  2. Priority Interview ( HackerEarth )
  3. Largest Square ( HackerEarth )
  4. Left of Right ( HackerEarth )
  5. Magic Square ( HackerEarth )
Summary

In this module, we have learned about Multidimensional Arrays and their implementation in problem-solving.

If you have any content or any doubt related to this module, mention in the comment section.

Thank You for Reading

Comments