# Multidimensional Arrays

In C Language one can have arrays of any dimensions. To understand the concept of multidimensional arrays let us consider the following 4 X 5 matrix

Row number (i)  Column numbers (j)
0 11  3 5 -9  -6
1 5 6 -8  7 24
2 -8  9 2 12  45
3 10  13   -10  4 5

Let us assume the name of matrix is x
To access a particular element from the array we have to use two subscripts one for row number and other for column number the notation is of the form X [i] [j] where i stands for row subscripts and j stands for column subscripts. Thus X [0] [0] refers to 10, X [2] [1] refers to 16 and so on In short multi dimensional arrays are defined more or less in the same manner as single dimensional arrays, except that for subscripts you require two squire two square brackets. We will restrict our decision to two dimensional arrays.

Below given are some typical two-dimensional array definitions

float table [50] [50]; char line [24] [40];
The first example defines tables as a floating point array having 50 rows and 50 columns. the number of elements will be 2500 (50 X50).

The second declaration example establishes an array line of type character with 24 rows and 40 columns. The number of elements will be (24 X 40) 1920 consider the following two dimensional array definition int values [3] [4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10. 11, 12, };

Values [0] [0] = 1 Values [0] [1] = 2 Values [0] [2] = 3 Values [0] [3] = 4 Values [1] [0] = 5 Values [1] [1] = 6 Values [1] [2] = 7 Values [1] [3] = 8 Values [2] [0] = 9 Values [2] [1] = 10 Values [2] [2] = 11 Values [2] [3] = 12

Here the first subscript stands for the row number and second one for column number. First subscript ranges from 0 to 2 and there are altogether 3 rows second one ranges from 0 to 3 and there are altogether 4 columns.
Alternatively the above definition can be defined and initialised as

int values [3] [4] = {
{
1, 2, 3, 4
}
{
5, 6, 7, 8
}
{
9, 10, 11, 12
}
};

Here the values in first pair of braces are initialised to elements of first row, the values of second pair of inner braces are assigned to second row and so on. Note that outer pair of curly braces is required. If there are two few values within a pair of braces the remaining elements will be assigned as zeros.

example program to add two matrics & store the results in the 3rd matrix

#include< stdio.h>
#include< conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,m,n,p,q;
clrscr();
printf("enter the order of the matrix\n");
scanf("%d%d",&p,&q);
if(m==p && n==q)
{
printf("matrix can be added\n");
printf("enter the elements of the matrix a");

for(i=0;i < m;i++)
for(j=0;j < n;j++)
scanf("%d",&a[i][j]);

printf("enter the elements of the matrix b");
for(i=0;i < p;i++)
for(j=0;j < q;j++)
scanf("%d",&b[i][j]);

printf("the sum of the matrix a and b is");

for(i=0;i < m;i++)
for(j=0;j < n;j++)
c[i][j]=a[i][j]+b[i][j];
for(i=0;i < m;i++)
{
for(j=0;j < n;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}

}
else
printf("Addition is not possible");
}

Copyright © 2018-2020 TutorialToUs. All rights reserved.