Array

Three dimension Array in Cpp language

Three dimension Array in Cpp language

In this tutorial, We will learn  about Three dimension Array in Cpp language

In the C++ Programming Language, an array is a fixed sequential collection of elements of the same data type. An array can be used to represent a list of numbers(int) or names (string) or other data type of similar elements. It is one of the ways of grouping similar type data with single variables names.

Three types of arrays in Cpp programming language

1.  One dimensional array

2. Two-dimensional array

3. Multi-dimensional array

 

Three dimensional Array


Now, we will look at three dimensional Array in C++. C++ programming supports multidimensional Array. The multidimensional array is also called a matrix. Here, we will declare the three-dimensional array.

 

Three Dimension array in CPP

Overview of three dimension array

Overview 3 D Array

 

How to find the index of the element in 3 D array

Find index 3 D array

Declaration of Three dimension Array

Syntax

Data_type Array_name[size 1][size 2][size 3];
Example 1 –  integer Array
int marks[2][3][4];
Example 2 – char Array
char alphabet[3][4][5];
Initialization of three dimension array in C++
int marks[2][3][4]={
      {{34,56,76,54},{98,53,86,34},{23,45,67,89}},
      {{8,87,76,65},{75,46,52,32},{78,65,84,62}}
};

Examples

Access elements in the array

insert elements from the array in C++

array name[index][index][index]=value;
marks[0][0][0]=45;

display elements from the array in C++

cout<<array name[index][index][index];
cout<<marks[0][0][0];

Program 1

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int marks[2][2][3];
    //array index must be index to Zero
    marks[0][0][0]=45;
    marks[0][0][1]=67;
    marks[0][0][2]=23;
    marks[0][1][0]=98;
    marks[0][1][1]=15;
    marks[0][1][2]=70;
    marks[1][0][0]=87;
    marks[1][0][1]=76;
    marks[1][0][2]=65;
    marks[1][1][0]=63;
    marks[1][1][1]=50;
    marks[1][1][2]=37;
    cout << "marks[0][0][0] :" << marks[0][0][0]<<"\n";
    cout << "marks[0][0][1] :" << marks[0][0][1]<<"\n";
    cout << "marks[0][0][2] :" << marks[0][0][2]<<"\n";
    cout << "marks[0][1][0] :" << marks[0][1][0]<<"\n";
    cout << "marks[0][1][1] :" << marks[0][1][1]<<"\n";
    cout << "marks[0][1][2] :" << marks[0][1][2]<<"\n";
    cout << "marks[1][0][0] :" << marks[1][0][0]<<"\n";
    cout << "marks[1][0][1] :" << marks[1][0][1]<<"\n";
    cout << "marks[1][0][2] :" << marks[1][0][2]<<"\n";
    cout << "marks[1][1][0] :" << marks[1][1][0]<<"\n";
    cout << "marks[1][1][1] :" << marks[1][1][1]<<"\n";
    cout << "marks[1][1][2] :" << marks[1][1][2]<<"\n";
    getch();
    return 0;
}

When the above code is executed, it produces the following results:

    marks[0][0][0]=45;
    marks[0][0][1]=67;
    marks[0][0][2]=23;
    marks[0][1][0]=98;
    marks[0][1][1]=15;
    marks[0][1][2]=70;
    marks[1][0][0]=87;
    marks[1][0][1]=76;
    marks[1][0][2]=65;
    marks[1][1][0]=63;
    marks[1][1][1]=50;
    marks[1][1][2]=37;

 

Program 2

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{

    cout << "Three dimension array in C++" << endl;
    int marks[2][2][3]={
        {
            {56,78,67},{56,78,43}},
            {{26,74,76},{36,58,90}},
        };
        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 results:

Three dimension array in Cpp
56
78
67
56
78
43
26
74
76
36
58
90

 

 

Display element of Array using nested for loop

 

Program 1

#include <iostream>
using namespace std;
int main()
{   int i,j,k;
    int marks[3][3][3]=
    {               //declaration and initiation
                {
                    {56,78,56},
                    {54,34,32},
                    {43,26,92},
                    },
                {   {48,94,62},
                    {41,39,66},
                    {68,40,90},
                    },
                {
                    {20,67,84},
                    {52,51,72},
                    {60,53,59},}
                    };
cout<<("3 D Array element\n")<<endl;
for(i=0;i<3;i++)
{
    for(j=0;j<3;j++)
    {
        for(k=0;k<3;k++){
            cout <<(marks[i][j][k]);
        }
        cout <<("\n");
    }
    cout <<("\n");
}
cout << "End of the program" << endl;
return 0;
}

 

When the above code is executed, it produces the following results:

3 D Array element

56,78,56
54,34,32
43,26,92

48,94,62
41,39,66
68,40,90
      
20,67,84
52,51,72
60,53,59

End of the program

 

Input and display element of Array using nested for loop

store elements to the array in C++ using for loop

for(int i=0; i<max_array_index; i++){
                   for(int j=0; j<max_array_index; j++){
                             for(int j=0; j<max_array_index; j++){
                                      cin>>array_name[i][j][k];
                    }
                    }
}

display elements from the array in C++ using for loop

for(int i=0; i<max_array_index; i++){
                   for(int j=0; j<max_array_index; j++){
                             for(int j=0; j<max_array_index; j++){
                                      cout<<array_name[i][j][k];
                    }
                    }
}

C++ program to store the value entered by the user in the three-dimensional array using nested for loop

Program 1

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    string Name[2][2][3];//Array declaration
    cout << "Enter your Names\n" << endl;
    for(int i=0; i<=1; i++){//input value using for loop
            for(int j=0; j<=1; j++){
                    for(int k=0; k<=2; k++){
       cout << "Name["<<i<<"]["<<j<<"]["<<k<<"] :";
    cin>> Name[i][j][k];//Ask input for element of array
                    }
            }
    }
    cout << "\nyour input name here" << endl;
    for(int i=0; i<=1; i++){//Display value using for loop
            for(int j=0; j<=1; j++){
                    for(int k=0; k<=2; k++){
       cout << "Name["<<i<<"]["<<j<<"]["<<k<<"] :"<<Name[i][j][k]<< endl;
           //Display element from Array
}
            }
    }
    getch();
    return 0;
}

 

 

When the above code is executed, it produces the following results:

Enter your names

Name[0][0][0] :Sathu
Name[0][0][1] :Khan
Name[0][0][2] :Nilu
Name[0][1][0] :Suthu
Name[0][1][1] :Kuru
Name[0][1][2] Jhon
Name[1][0][0] :Sam
Name[1][0][1] :Sumo
Name[1][0][2] :Remi
Name[1][1][0] :Ragu
Name[1][1][1] :Thivya
Name[1][1][2] :Thurai

Your input name here
Name[0][0][0] :Sathu
Name[0][0][1] :Khan
Name[0][0][2] :Nilu
Name[0][1][0] :Suthu
Name[0][1][1] :Kuru
Name[0][1][2] Jhon
Name[1][0][0] :Sam
Name[1][0][1] :Sumo
Name[1][0][2] :Remi
Name[1][1][0] :Ragu
Name[1][1][1] :Thivya
Name[1][1][2] :Thurai

 

Display element of Array using nested while loop

Program 1

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    int marks[2][3][3]=//array declaration
    {
       {{34,67,54},{37,73,80},{27,54,56} },
       {{67,86,33},{52,47,69},{23,42,46} },
    };

    cout << "Display your marks here :" << endl;
    int m=0;
    while(m<=1){
        int n=0;
        while(n<=2){
                int l=0;
                while(l<=1){
            cout <<"marks["<<m<<"]["<<n<<"]"<<l<<"]="<<marks[m][n][l]<< endl;
            l++;
        }
        n++;
    }
    m++;
    }
    cout << "End the program" << endl;
    getch();
    return 0;
}


 

When the above code is executed, it produces the following results:

Display your marks here :
marks[0][0][0]=34
marks[0][0][1]=67
marks[0][1][0]=37
marks[0][1][1]=73
marks[0][0][0]=27
marks[0][0][1]=54
marks[1][1][0]=67
marks[1][1][1]=86
marks[1][0][0]=52
marks[1][0][1]=47
marks[1][1][0]=23
marks[1][1][1]=42
End the program

 

Input and display element of Array using nested while loop

store elements in the array in C++ using while loop

int i=0;
while(i<=max_array_index){
      int j=0;
      while(j<=max_array_index){
                   intk=0;
                    while(j<=max_array_index){
                      cin>>array_name[i][j][k];
         }
k++;
     }
j++;
}
i++;

 

display elements from the array in C++ using while loop

int i=0;
while(i<=max_array_index){
      int j=0;
      while(j<=max_array_index){
                   intk=0;
                    while(j<=max_array_index){
                      cout<<array_name[i][j][k];
         }
k++;
         }
j++;
}
i++;

C++ program to store the value entered by the user in the three-dimensional array using while loop

Program 1

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    string names[2][3][2];
    cout <<"Enter names"<< endl;
    int i=0;
    while(i<=1){
        int j=0;
        while(j<=2){
            int k=0;
                while(k<=1){
            cout <<"names["<<i<<"]["<<j<<"]["<<k<<"]=";
            cin>>names[i][j][k];
            k++;
                }
                j++;
    }
    i++;
    }
    cout << "\n This names are stored in array :" << endl;
    int m=0;
    while(m<=1){
        int n=0;
        while(n<=2){
                int l=0;
                while(l<=1){
            cout <<"names["<<m<<"]["<<n<<"]"<<l<<"]="<<names[m][n][l]<< endl;
            l++;
        }
        n++;
    }
    m++;
    }
    cout << "End of program" << endl;
    getch();
    return 0;
}

 

When the above code is executed, it produces the following results:

Enter names
names[0][0][0]=Saru
names[0][0][1]=Kuna
names[0][1][0]=Joe
names[0][1][1]=Jhon
names[0][2][0]=Pilib
names[0][2][1]=Para
names[1][0][0]=Mohi
names[1][0][1]=Suba
names[1][1][0]=Guru
names[1][1][1]=Nila
names[1][2][0]=Mala
names[1][2][1]=Mathu

This names are stored in array
names[0][0][0]=Saru
names[0][0][1]=Kuna
names[0][1][0]=Joe
names[0][1][1]=Jhon
names[0][2][0]=Pilib
names[0][2][1]=Para
names[1][0][0]=Mohi
names[1][0][1]=Suba
names[1][1][0]=Guru
names[1][1][1]=Nila
names[1][2][0]=Mala
names[1][2][1]=Mathu

 

 

Similar post

Single dimension Array in C++ language

Two dimension Array in C++ language

Three dimension Array in C++ language

Single dim- Array in Java language

Two dim- Array in Java language

Three dim- Array in Java language

Single dim- Array in C language

Two dim- Array in C language

Three dim- Array in C language

 

Suggested for you

Nested for loop in C++ language

Nested while loop in C++ language

The new keyword in Java

for loop in C++ language

While loop in C++ language

 

Single dimension Array of C language
Two dimension Array in Cpp language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

Python code to Calculate sum of odd and even in a list

Python code to Calculate sum of odd and even in a list In this tutorial,…

17 hours ago

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

3 days ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago