8.15 – Multidimensional Arrays

by subbu on November 10, 2013

Multidimensional arrays in C language

As we know an array is a set of elements of similar type stored in adjacent memory allocations under a common name.

If an array has set of arrays as elements then the array is called multi-dimensional array. In case of a two dimensional array we specify the size with two subscripts. It is so convenient to think that the first subscript as rows, second subscript as columns and a two dimensional array as a table or matrix.

In case of a three dimensional array, we specify the size with three subscripts. Now it is convenient to think the first subscript as tables, second subscript as rows, third subscript as columns and a 3dimensional array as a set of tables.

We can define n dimensions as per the requirement but, the maximum dimensions allowed by the C language are compiler dependant. Say for example specification of C11 suggests maximum of 12 dimensions.

While initializing a single dimensional array, it is optional specifying the size.

int x[]={3,4,5,6,7};

While initializing a two dimensional array, it is optional specifying the 1st dimension that is the number of rows

int y[][4]={
             {4,6,7,8},
             {2,3,9,7},
             {9,0,8,5}
           };

While initializing a three dimensional array, It is optional specifying the 1st dimension that is the number of tables.

int z[][2][4]={
                {{6,2,5,6},{1,4,9,3}},
                {{5,3,9,0},{1,2,6,7}},
                {{8,6,1,5},{3,5,2,4}}
              };

Here z is a three dimensional array with three matrices of 2×4. Every element in a three dimensional array is accessed using three subscripts. The first subscript is the table index, second subscript is the row index and the third subscript is the column index.

three dimensional array

Loop to traverse a 3dimensional array:

for(k=0;k<t;k++)   /* to select tables */
{
   for(i=0;i<n;i++)
   {
      for(j=0;j<m;j++)
          printf("%5d",a[k][i][j]);
      printf("\n");
   }
   printf("\n\n");
}

Program:

#include<stdio.h>
int main()
{
int z[][2][4]={
               {{6,2,5,6},{1,4,9,3}},
               {{5,3,9,0},{1,2,6,7}},
               {{8,6,1,5},{3,5,2,4}}
              };
int k,i,j;
printf("Elements of data cube:\n");
for(k=0;k<3;k++)   /* to select tables */
{
   for(i=0;i<2;i++)
   {
      for(j=0;j<4;j++)
         printf("%5d",z[k][i][j]);
      printf("\n");
   }
   printf("\n\n");
}
return 0;
}

Output:
Elements of data cube:
6    2    5    6
1    4    9    3

5    3    9    0
1    2    6    7

8    6    1    5
3    5    2    4

Discourage of using multidimensional arrays:

In C language, size of array is specified while writing the program. We may use few elements of the declared array; left all the elements are unused. It leads to the wastage of memory. There would be more wastage of memory as the number of dimensions increases.

Say for example in a single dimensional array a[50], if we use 10 elements then there would be 40 unused elements. In a two dimensional array a[50][50], if we use only 10×10 then there would be 40×40 (1600) unused elements. In case a three dimensional array a[50][50][50], if we use 10x10x10 elements there would be 40x40x40 (64000) unused elements.

The second disadvantage of using multidimensional array is reducing the performance as the number of dimensions increase. This we will discourse in another session under pointer arithmetics

Hence it is suggested to discourage using multidimensional arrays to save the memory and improve the performance.

Misc. issues in array initialization:

1) If an array of unknown size is initialized then its size is determined by the largest indexed element.

int[]={4,8,3,9,2,12};

initializing a single dimensional array

Here the index of last element is 5 hence the size of array would be 6.

2) While initializing an array, if few elements are supplied than the given size then un-initialized elements are assigned with zeros

#include<stdio.h>
int main()
{
 int a[6]={3,4,6,7};    /* shortage of elements */
 int i;
 for(i=0;i<6;i++)
  printf("%5d",a[i]);
 return 0;
}

Output:
3    4    6    7    0    0

3) While initializing an array, if more elements are supplied than the given size then it results a warning

#include<stdio.h>
int main()
{
int a[6]={3,4,6,7,9,60,34,23};   /* excess number of elements */
int i;
for(i=0;i<6;i++)
  printf("%5d",a[i]);
return 0;
}

Output:
warning: excess elements in array initializer

4) While initializing an array, if any element is missing in the set then it may result error

#include<stdio.h>
int main()
{
 int a[6]={3,4,6,,7,9};  /* missing element */
 int i;
 for(i=0;i<6;i++)
   printf("%5d",a[i]);
 return 0;
}

Output:
subbu7.c:4:20: error: expected expression before ‘,’ token
int a[6]={3,4,6,,7,9};

5)

int a[4][3]={
             {1,3,5},
             {2,4,6},
             {3,5,7}
           };

initializing a single dimensional array

Here a is a 4×3 matrix but initialized only with three rows. Hence the last row is initialized with zeros.

6)

#include<stdio.h>
int main()
{
int a[4][3]={
             {1,3,5},
             {2,4,6},
             {3,5,7}
            };
int i,j;
printf("Elements of matrix are:\n");
for(i=0;i<4;i++)
{
  for(j=0;j<3;j++)
    printf("%5d",a[i][j]);
  printf("\n");
}
return 0;
}

Output:
Elements of matrix are:
1    3    5
2    4    6
3    5    7
0    0    0

7)

int a[4][3]={1,3,5,2,4,6,3,5,7};

initializing a single dimensional array

Here we have not even separated the rows with braces but elements are assigned row by row and unassigned row is assigned with 0’s because C language is a row major language.

8)

#include<stdio.h>
int main()
{
int a[4][3]={1,3,5,2,4,6,3,5,7};
int i,j;
printf("Elements of matrix are:\n");
for(i=0;i<4;i++)
{
   for(j=0;j<3;j++)
     printf("%5d",a[i][j]);
   printf("\n");
}
return 0;
}

Output:
Elements of matrix are:
1    3    5
2    4    6
3    5    7
0    0    0

9)

int a[4][3]={
             {1},
             {3,5,2},
             {4,6},
             {3,5,7}
            };

juggled array

Here 4 rows are initialized with insufficient number of elements. Unspecified elements are initialized with 0s.

10)

#include<stdio.h>
int main()
{
int a[4][3]={
             {1},
             {3,5,2},
             {4,6},
             {3,5,7}
            };
int i,j;
printf("Elements of matrix are:\n");
for(i=0;i<4;i++)
{
   for(j=0;j<3;j++)
     printf("%5d",a[i][j]);
   printf("\n");
}
return 0;
}

Output:
Elements of matrix are:
1    0    0
3    5    2
4    6    0
3    5    7

Previous post:

Next post: