Table of Contents
Three dimensional Array in C language
In this tutorial, we will discuss the Three Dimensional Array in C language
Knowledge Area
- What is Array
- Type of Arrays
- Declaration of 3 dimensional Array
- 3 dimensional Array Initialization
- 3 dimensional Array processing
In the C Programming Language, an array is a fixed sequenced collection of the element of the same data types. an array can be used to represent a list of number(int) or name (string) or other data type of similar elements. It is one of the ways of simply a grouping of similar types of data of single variables names.
Three types of arrays in C programming language
1. One dimensional array
2. Two-dimensional array
3. multidimensional array
Three dimensional Array
now we will understand the three-dimensional Array in C. C programming supports multidimensional Array. The Multidimensional array is also called a matrix
Here we will learn how to declare, initialize and access the multidimensional array
![" #include <stdlib.h> int main() { int marks[2][2][3];//3 D Array declaration //initializing element marks[0][0][0]=68; marks[0][0][1]=78; marks[0][0][2]=76; marks[0][1][0]=89; marks[0][1][1]=84; marks[0][1][2]=47; marks[1][0][0]=65; marks[1][0][1]=24; marks[1][0][2]=45; marks[1][1][0]=54; marks[1][1][1]=72; marks[1][1][2]=35; //display array element based in index printf(](https://i0.wp.com/1.bp.blogspot.com/-pYx9_rTRKA4/WDqA0qEJPcI/AAAAAAAABD8/Rv9DkjZixHoSzDnxAZQzANPIIR_el8liQCLcB/s320/3da.jpg?resize=328%2C326&ssl=1)
Declare a three-dimensional array
Syntax
data_type array_name[size1][size2][size3];
Example
int marks[2][3][4];
Initializing – using standard method
Program 1
#include <stdio.h>
#include <stdlib.h>
int main()
{
int marks[2][2][3];//3 D Array declaration
//initializing element
marks[0][0][0]=68;
marks[0][0][1]=78;
marks[0][0][2]=76;
marks[0][1][0]=89;
marks[0][1][1]=84;
marks[0][1][2]=47;
marks[1][0][0]=65;
marks[1][0][1]=24;
marks[1][0][2]=45;
marks[1][1][0]=54;
marks[1][1][1]=72;
marks[1][1][2]=35;
//display array element based in index
printf("Here, display array elements\n\n");
printf("marks[0][0][0] value is: %d\n",marks[0][0][0]);
printf("marks[0][0][1] value is: %d\n",marks[0][0][1]);
printf("marks[0][0][2] value is: %d\n",marks[0][0][2]);
printf("marks[0][1][0] value is: %d\n",marks[0][1][0]);
printf("marks[0][1][1] value is: %d\n",marks[0][1][1]);
printf("marks[0][1][2] value is: %d\n",marks[0][1][2]);
printf("marks[1][0][0] value is: %d\n",marks[1][0][0]);
printf("marks[1][0][1] value is: %d\n",marks[1][0][1]);
printf("marks[1][0][2] value is: %d\n",marks[1][0][2]);
printf("marks[1][1][0] value is: %d\n",marks[1][1][0]);
printf("marks[1][1][1] value is: %d\n",marks[1][1][1]);
printf("marks[1][1][2] value is: %d\n",marks[1][1][2]);
getch();
return 0;
}
Wen the above code is executed,it produces te following result
![" #include <stdlib.h> int main() { int array[3][4][2]={{{2,4},{5,7},{8,9},{1,0}}, {{3,9},{4,7},{4,3},{1,9}}, {{7,5},{3,8},{5,6},{2,1}} }; printf(](https://i0.wp.com/code4javac.com/wp-content/uploads/2016/12/arrayoutput.jpg?resize=436%2C386&ssl=1)
Initializing during the array declaration
Program 2
#include <stdio.h>
#include <stdlib.h>
int main()
{
int marks[2][3][2]={
{{34,57},{77,79},{64,68}},
{{62,53},{25,83},{36,76}},
};
printf("marks[0][0][0]=%d\n", marks[0][0][0]);
printf("marks[0][0][1]=%d\n", marks[0][0][1]);
printf("marks[0][1][0]=%d\n", marks[0][1][0]);
printf("marks[0][1][1]=%d\n", marks[0][1][1]);
printf("marks[0][2][0]=%d\n", marks[0][2][0]);
printf("marks[0][2][1]=%d\n", marks[0][2][1]);
printf("marks[1][0][0]=%d\n", marks[1][0][0]);
printf("marks[1][0][1]=%d\n", marks[1][0][1]);
printf("marks[1][1][0]=%d\n", marks[1][1][0]);
printf("marks[1][1][1]=%d\n", marks[1][1][1]);
printf("marks[1][2][0]=%d\n", marks[1][2][0]);
printf("marks[1][2][1]=%d\n", marks[1][2][1]);
getch();
return 0;
}
Wen the above code is executed,it produces te following result
Initializing and displaying using loops
using for loop
Program 1
#include <stdio.h>
#include <stdlib.h>
int main()
{
int stu_Marks[2][3][2];//3 D array declaration in C
int i,j,k;//counter variable declaration
printf("Enter the elements of the array\n");
for(i=0; i<2; i++){
for(j=0; j<3; j++){
for(k=0; k<2; k++){
printf("Enter value of marks[%d][%d][%d]=",i,j,k);
scanf("%d",&stu_Marks[i][j][k]);
}
}
}
printf("\nDisplay entered elements\n");
for(i=0; i<2; i++){
for(j=0; j<3; j++){
for(k=0; k<2; k++){
printf("stu_Marks[%d][%d][%d]=%d\n",i,j,k,stu_Marks[i][j][k]);
}
}
}
getch();
return 0;
}
Wen the above code is executed,it produces te following result
![" #include <stdlib.h> int main() { int stu_Marks[2][3][2];//3 D array declaration in C int i,j,k;//counter variable declaration printf(](https://i0.wp.com/code4javac.com/wp-content/uploads/2016/12/3doutput1.jpg?resize=399%2C489&ssl=1)
Program 2
using while loop
#include <stdio.h>
#include <stdlib.h>
int main()
{
int stu_Marks[2][3][2];//3 D array declaration in C
int i,j,k;//counter variable declaration
printf("Enter the elements of the array\n");
i=0;
while(i<2){
j=0;
while(j<3){
k=0;
while( k<2){
printf("Enter value of marks[%d][%d][%d]=",i,j,k);
scanf("%d",&stu_Marks[i][j][k]);
k++;
}
j++;
}
i++;
}
printf("\nDisplay entered elements\n");
i=0;
while(i<2){
j=0;
while(j<3){
k=0;
while(k<2){
printf("stu_Marks[%d][%d][%d]=%d\n",i,j,k,stu_Marks[i][j][k]);
k++;
}
j++;
}
i++;
}
getch();
return 0;
}
Wen the above code is executed,it produces te following result
![" #include <stdlib.h> int main() { int stu_Marks[2][3][2];//3 D array declaration in C int i,j,k;//counter variable declaration printf(](https://i0.wp.com/code4javac.com/wp-content/uploads/2016/12/output3dc.jpg?resize=396%2C435&ssl=1)
Program 3
using do-while loop
#include <stdio.h>
#include <stdlib.h>
int main()
{
int stu_Marks[2][3][2];//3 D array declaration in C
int i,j,k;//counter variable declaration
printf("Enter the elements of the array\n");
i=0;
do{
j=0;
do{
k=0;
do{
printf("Enter value of marks[%d][%d][%d]=",i,j,k);
scanf("%d",&stu_Marks[i][j][k]);
k++;
}while( k<2);
j++;
}while(j<3);
i++;
}while(i<2);
printf("\nDisplay entered elements\n");
i=0;
do{
j=0;
do{
k=0;
do{
printf("stu_Marks[%d][%d][%d]=%d\n",i,j,k,stu_Marks[i][j][k]);
k++;
}while(k<2);
j++;
}while(j<3);
i++;
}while(i<2);
getch();
return 0;
}
Wen the above code is executed,it produces te following result

![" #include <stdlib.h> int main() { int stu_Marks[2][3][2];//3 D array declaration in C int i,j,k;//counter variable declaration printf(](https://i0.wp.com/4.bp.blogspot.com/-drRXAR9g-20/WE42pC4QpXI/AAAAAAAABTg/D3JZQ1V-27IEf41dMICOWGiJ9ANyJpwpQCLcB/s640/3darray1.jpg?resize=640%2C750&ssl=1)
