# Multidimensional Arrays in C

C++Server Side ProgrammingProgramming

Here we will see the multidimensional arrays. An array is basically a set of homogeneous data. They are placed into contiguous memory locations. In different cases we can see that arrays are not one dimensional. Sometimes we need to create an array in two-dimensional or multidimensional form.

The multidimensional arrays can be represented by two different approaches. These are Row-Major approach, and another is Column-Major approach. Consider a two-dimensional array with r rows and c columns. The number of elements in the array is n = r * c. The elements at position A[i, j], where 0 ≤ i < r and 0 ≤ j < c, will be mapped onto an integer in the range [0, n1]. If it is represented in row-major order, then elements in row 0 are listed in the order from left to right followed by the elements of row 1, row 2 and so on. Here the mapping function is (ic + j). If the elements are listed in column major order, then mapping function is (i + jr)

## Example

Live Demo

#include <stdio.h>
int main () {
/* an array with 5 rows and 2 columns*/
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
int i, j;
/* output each array element's value */
for ( i = 0; i < 5; i++ ) {
for ( j = 0; j < 2; j++ ) {
printf("a[%d][%d] = %d\n", i,j, a[i][j] );
}
}
return 0;
}

## Output

a[0][0]: 0
a[0][1]: 0
a[1][0]: 1
a[1][1]: 2
a[2][0]: 2
a[2][1]: 4
a[3][0]: 3
a[3][1]: 6
a[4][0]: 4
a[4][1]: 8
Published on 10-Aug-2020 08:13:19