Three dimensional Array in C language
In this tutorial, we will learn the concept of Three Dimensional Array in C++ language
In this post, we will learn how to declare, create, initialize and access Three Dimensional Array in C++ language
Table of Contents
Knowledge Area
- What is Array
- Type of Arrays
- Declaration of three dimensional Array in C++
- Three dimensional Array Initialization in C++
- Three dimensional Array processing in C++
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 dim Array
Now we will know three dim Array in C++. C++ programming supports multidimensional Array. the Multidimensional array is also called as matrix Here we will declare the three-dim array
Explanation of three dimenssion Array
A 3d Array is essentially an array of arrays of arrays or collection of 2 D Arrays.
Declaration three dim Array
Syntax
Data _Type Array_name[size1][size2][size3];
Example1 – integer Array
int marks[2][3][4];
Example2 – character Array
char volume[20][30][40];
Initialization of Three dim Array
Method 1
Initialize using standred method
We can initialized every element in three dim array
marks[0][0][0]=45; //Cell address of marks[][][] have one elemet value is 45
Example
#include <iostream> #include <conio.h> using namespace std; int main() { int marks[2][2][3];//array declaration marks[0][0][0]=78; //initialization of array of 2 D array marks[0][0][1]=68; marks[0][0][2]=35; marks[0][1][0]=79; marks[0][1][1]=52; marks[0][1][2]=49; marks[1][0][0]=45; marks[1][0][1]=73; marks[1][0][2]=63; marks[1][1][0]=45; marks[1][1][1]=73; marks[1][1][2]=63; //Display elements cout << "Array elements display here\n" << endl; cout << marks[0][0][0] << endl; cout << marks[0][0][1] << endl; cout << marks[0][0][2] << endl; cout << marks[0][1][0] << endl; cout << marks[0][1][1] << endl; cout << marks[0][1][2] << endl; cout << marks[1][0][0] << endl; cout << marks[1][0][1] << endl; cout << marks[1][0][2] << endl; cout << marks[1][1][0] << endl; cout << marks[1][1][1] << endl; cout << marks[1][1][2] << endl; getch(); return 0; }
When the above code is executed it produces the following result
Initialized during the declaration
int marks[2][3][4]={
{{45,65,34,78},{78,56,32,23},{68,57,37,89}},
{{87,43,68,53},{“62,67,43,87},{54,67,90,87}}
};
Program 1
#include <iostream>
using namespace std;
int main()
{
int i, j, k;
int arr[3][3][3]=
{
{
{14, 16, 13},
{14, 15, 19},
{17, 18, 89}
},
{
{51, 22, 73},
{24, 95, 26},
{57, 28, 79}
},
{
{33, 30, 39},
{35, 45, 56},
{99, 38, 37}
},
};
cout<<(“:::3D Array Elements:::nn”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
cout<<(arr[i][j][k]);
}
cout<<(“n”);
}
cout<<(“n”);
}
}
When the above code is executed it produces the following result
Initialize and display elements using loops
#include <iostream> #include <conio.h> using namespace std; int main() { int arr[2][2][3],i,j,k; cout << "Enter the numbers for 3 D array" << endl; for(i=0; i<2; i++){ for(j=0; j<2; j++){ for(k=0; k<3; k++){ cin>>arr[i][j][k];//store array elements } } } cout << "\nYour entered elements display here\n" << endl; for(i=0; i<2; i++){ for(j=0; j<2; j++){//display the array elements for(k=0; k<3; k++){//display the array elements cout << "you entered to index arr["<<i<<"]["<<j<<"]["<<k<<"]: "<<arr[i][j][k] <<endl; } } } getch(); return 0; }
When the above code is executed it produces the following result
#include <iostream> #include <conio.h> using namespace std; int main() { int arr[2][2][3],i,j,k; cout << "Enter the numbers for 3 D array" << endl; i=0; while(i<2){ j=0; while(j<2){ k=0; while(k<3){ cin>>arr[i][j][k];//store array elements k++; } j++; } i++; } cout << "\nYour entered elements display here\n" << endl; i=0; while(i<2){ j=0; while(j<2){//display the array elements k=0; while(k<3){//display the array elements cout << "you entered to index arr["<<i<<"]["<<j<<"]["<<k<<"]: "<<arr[i][j][k] <<endl; k++; } j++; } i++; } getch(); return 0; }
When the above code is executed it produces the following result
#include <iostream> #include <conio.h> using namespace std; int main() { int arr[2][2][3],i,j,k; cout << "Enter the numbers for 3 D array" << endl; i=0; do{ j=0; do{ k=0; do{ cin>>arr[i][j][k];//store array elements k++; } while(k<3); j++; }while(j<2); i++; }while(i<2); cout << "\nYour entered elements display here\n" << endl; i=0; do{ j=0; do{//display the array elements k=0; do{//display the array elements cout << "you entered to index arr["<<i<<"]["<<j<<"]["<<k<<"]: "<<arr[i][j][k] <<endl; k++; }while(k<3); j++; }while(j<2); i++; }while(i<2); getch(); return 0; }
When the above code is executed it produces the following result
One dim Array in Java One dim Array in C++ One dim Array in C
Two dim Array in Java Two dim Array in C++ Two dim Array in C
Three dim Array in Java Three dim Array in C++ Three dim Array in C