MULTI DIMENSIONAL ARRAYS


MULTI DIMENSIONAL ARRAYS

            Like one-dimensional and two dimensional arrays C language also allows multi-dimensional arrays. Arrays with three or more dimensions are called multi-dimensional arrays.
A multi dimensional array can be declared as follows:-

Syntax:
            datatype variable[size1][size2]……[size n];

ex:  int a[4][5][4];
Initializing values to multidimensional arrays.

Eg:-
            int a[3][3][3]={{{1,2,3},
             {4,5,6},
             {7,8,9}},
          {{10,11,12},
         {13,14,15}
         {16,17,18}},
                                    {{19,20,21},
         {22,23,24},
       {25,26,27}}};

Sample program that reads and prints elements of a three dimensional array.

#include<stdio.h>
#include<conio.h>
void main()
{
 int i,j,k,a[10][10][10],m,n,p;
 clrscr();
 printf("\n Enter order of matrix: ");
 scanf("%d%d%d",&m,&n,&p);
 printf(" Enter elements: ");
 for(i=0;i<m;i++)
 {
  for(j=0;j<n;j++)
  {
   for(k=0;k<p;k++)
   {
    scanf("%d",&a[i][j][k]);
   }
  }
 }
 printf(" Elements in matrix form are :");
 for(i=0;i<m;i++)
 {
  printf("\n");
  for(j=0;j<n;j++)
  {
   for(k=0;k<p;k++)
   {
    printf("%d\t",a[i][j][k]);
   }
   printf("\n");
  }
 }
 getch( );
}



Output

 Enter order of matrix: 2
2
2
 Enter elements: 1
2
3
4
5
6
7
8
 Elements in matrix form are :
1       2
3       4

5       6
7       8


No comments:

Post a Comment