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
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
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
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
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