Three dimensional array in C++

bookmark

#include <iostream>
using namespace std;

int main(){
	int a, b, c;
	cout << "Enter the Size of array\n";	//taking input for the size of array
	cin >> a >> b >> c;
	int arr[a][b][c];	//array of required size declared

	for (int i = 0; i < a; ++i)	//counter for first dimension
	{
		for (int j = 0; j < b; ++j)	//counter for second dimension
		{
			for (int k = 0; k < c; ++k)	//counter for third dimension
			{
				cout << "\nEnter value at position[" << i << "]" << "[" << j << "]" << "[" << k << "]";

				cin >> arr[i][j][k];	//taking input in the set counter
			}
		}
	}

	for (int i = 0; i < a; ++i)	//printing the array values as set
	{
		for (int j = 0; j < b; ++j)
		{
			for (int k = 0; k < c; ++k)
			{
				cout << "\nValue at position[" << i << "]" << "[" << j << "]" << "[" << k << "]= " << arr[i][j][k];
			}
		}
	}
	return 0;
}

 

Output

Enter the Size of array
2 3 2

Enter value at position[0][0][0]
5
Enter value at position[0][0][1]
3
Enter value at position[0][1][0]
4
Enter value at position[0][1][1]
8
Enter value at position[0][2][0]
1
Enter value at position[0][2][1]
3
Enter value at position[1][0][0]
6
Enter value at position[1][0][1]
9
Enter value at position[1][1][0]
2
Enter value at position[1][1][1]
5
Enter value at position[1][2][0]
8
Enter value at position[1][2][1]
7

Value at position[0][0][0]= 5
Value at position[0][0][1]= 3
Value at position[0][1][0]= 4
Value at position[0][1][1]= 8
Value at position[0][2][0]= 1
Value at position[0][2][1]= 3
Value at position[1][0][0]= 6
Value at position[1][0][1]= 9
Value at position[1][1][0]= 2
Value at position[1][1][1]= 5
Value at position[1][2][0]= 8
Value at position[1][2][1]= 7