Multidimensional Arrays
Multidimensional arrays are arrays of arrays are declared.
A simple example is given below for 2-D array:
int int_array [ 10 ] [ 10 ] ; // A two dimensional array
- Initializing a two dimensional array
It can be initialized as follows :
int array [ 2 ] [ 2 ] = {1, 2, 3, 4} ;
- Accessing the elements
Elements in two dimensional arrays can be accessed as follows:
a [ 0 ] [ 0 ] = 10 ; // assigns 10 to element at first row first column
a [ 0 ] [ 1 ] = 1 ; // assigns 1 to element at first row second column
a [ 2 ] [ 2 ] = 11 ; // assigns 11 to element at third row third column
Implementation of a 2-D array
A two dimensional array can be implemented in a programming language in two ways :
1. Row major implementation
2. Column major implementation
Row major implementation
In it elements of array are read form keyboard row-wise i.e, complete first row is stored, then complete second row and so on.
Address of element, a[i][j] = B + W ( n ( i – L1 ) + ( j – L2))
Where, B – Base address
W – Size of each array element
n – Number of columns
L1 – Lower bound of row
L2 – Lower bound of column
Column major implementation
In it elements of array are read form keyboard column-wise i.e, complete first column is stored, then complete second column and so on.
Address of element, a[i][j] = B + W ( m ( j – L2 ) + ( i – L1))
Where,
B – Base address
W – Size of each array element
m – Number of rows
L1 – Lower bound of row
L2 – Lower bound of column