Three OR Multi dimensional array :
The c program allows array of two or multi dimensions.The compiler determines the restriction on it.The syntax of multi dimensional array as follows......Arrayname [s1][s2][s3]....[sn]
Where,si is the size of the ist dimensions
Three dimensional array can be initialise as follows..
int mat [3][3][3]={
{ 1,2,3,
4,5,6,
7,8,9,
1,4,7,
2,8,9,
1,2,3,
};
{
2,9,8,
4,1,3
3,2,3
}
A three dimensional array can be through of as an array of arrays.The outer array contains three elements.The inner array size is two dimensional with size [3][3].
Example:
#include<stdio.h>
#include<conio.h>
void main ( )
{
int array_3d[3][3][3];
int a,b,c;
for(a=0;a<3;a++)
for(b=0;b<3;b++)
for(c=0;c<3;c++)
array_3d[a][b][c]=a+b+c;
for(a=0;a<3;a++)
{
printf("\n");
for(b=0;b<3;b++)
{
for(c=0;c<3;c++)
printf("%3d",array_3d[a][b][c]);
printf("\n");
}
}
}
OUTPUT :
012
123
234
123
234
345
234
345
456
Explanation :
The three dimensional array array_3d is initialised.The first three for loops are used for adding the values of a,b &c.Here,initially a & b are zero and 'c' varies from 0 to 2.hence,the addition of a,b &c will be 0 1 2.This will be printed in the first row.the second output row in which a=0,b=1 and c varies from 0 to 2.Thus the output of the second row will be 1 2 3.in this way the values of a,b &c are changed and the total 2 iterations are carried out.