C++ Program to store value in Three Dimensional Array

147
views
Three Dimensional Array
(Last Updated On: October 15, 2018)

In this example, you will learn how to Store value entered by the user in Three Dimensional Array and display it.

To understand this Program of Three Dimensional Array, you can learn C++ programming.

  1. C++ I/O
  2. C++ for loop

A 3D array is essentially an array of arrays of arrays: it’s an array or collection of 2D arrays, and a 2D array is an array of 1D array.

It may sound a bit confusing, but don’t worry. As you practice working with multidimensional arrays, you start to grasp the logic.

In this program, we will learn how to Store value entered by the user in the three-dimensional array and display it.

Program to store value in Three Dimensional Array

#include <iostream>
using namespace std;

int main()
{
    // This array can store upto 12 elements (2x3x2)
    int test[2][3][2];

    cout << "Enter 12 values: \n";
    
    // Inserting the values into the test array
    // using 3 nested for loops.
    for(int i = 0; i < 2; ++i)
    {
        for (int j = 0; j < 3; ++j)
        {
            for(int k = 0; k < 2; ++k )
            {
                cin >> test[i][j][k];
            }
        }
    }

    cout<<"\nDisplaying Value stored:"<<endl;

    // Displaying the values with proper index.
    for(int i = 0; i < 2; ++i)
    {
        for (int j = 0; j < 3; ++j)
        {
            for(int k = 0; k < 2; ++k)
            {
                cout << "test[" << i << "][" << j << "][" << k << "] = " << test[i][j][k] << endl;
            }
        }
    }

    return 0;
}

Output

Enter 12 values: 
1
2
3
4
5
6
7
8
9
10
11
12

Displaying Value stored:
test[0][0][0] = 1
test[0][0][1] = 2
test[0][1][0] = 3
test[0][1][1] = 4
test[0][2][0] = 5
test[0][2][1] = 6
test[1][0][0] = 7
test[1][0][1] = 8
test[1][1][0] = 9
test[1][1][1] = 10
test[1][2][0] = 11
test[1][2][1] = 12

Ask your questions and clarify your/others doubts on Three Dimensional Arrays by commenting or posting on the forum. Documentation

Please write to us at [email protected] to report any issue with the above content or for feedback.

Related Programs