4-Dimensional Array in C/C++
A four-dimensional (4D) array is an array of three-dimensional (3D) arrays, or in other words we can say that a 4- dimensional array is an array of arrays of arrays of arrays. An array with more dimensions can hold more data, but it also makes managing and comprehending the array more challenging.
Syntax:
data_type array_name [i1] [i2] [i3] [i4] ……... [in];
here i represent dimension, and n represents dimension size.
Example
Let’s take int student [4] [5] [6] [7], where 4-d array is the student and int is array integer type, by multiplying it can hold 4*5*6*7 which is equals to 840.
Another example is float country [5] [6] [5] [6] [5], where five-dimensional array is the country which can hold 5*6*5*6*5 which is equals to 4500.
Code:
include <iostream>
usingnamespacestd;
intmain()
{
// indexes are used for the declaration of the variable
inti, j, k, l, size;
// declaration of an array
inta[2][2][2][2];
// array size
size = 2;
// elements for an input
a[0][0][0][0] = 5;
a[0][0][0][1] = 3;
a[0][0][1][0] = 5;
a[0][0][1][1] = 3;
a[0][1][0][0] = 6;
a[0][1][0][1] = 7;
a[0][1][1][0] = 6;
a[0][1][1][1] = 7;
a[1][0][0][0] = 8;
a[1][0][0][1] = 9;
a[1][0][1][0] = 8;
a[1][0][1][1] = 9;
a[1][1][0][0] = 9;
a[1][1][0][1] = 7;
a[1][1][1][0] = 9;
a[1][1][1][1] = 7;
// values printing
for(i = 0; i < size; i++) {
for(j = 0; j < size; j++) {
for(k = 0; k < size; k++) {
for(l = 0; l < size; l++) {
cout << "Value of a["<< i << "]["<< j << "]["<< k << "]["<< l
<< "]["<< l<< "] :- "<< a[i][j][k][l];
cout << "\n";
}
}
}
}
return0;
}
Output:
![4-Dimensional Array in C/C++](https://static.tutorialandexample.com/cpp/4-dimensional-array-in-c-or-cpp1.png)
Multidimensional arrays in C++ are mostly used to perplex new users and cause them to wonder how to dynamically allocate them, how to delete them, or why they can't be converted to arrows to arrows.
Use
If we give input of 3 coordinates and 1 time, i.e., x, y, z, and t, and wish to determine whether there is a collision between two cars or not, we can store the data in a 4D array.
An array of 3Darrays makes up a 4-dimensional array.
Algorithm
- Begin.
- Make the variables known.
- Indicate the array's components.
- Enter the number of items.
- Consider the components as input.
- Print the array's stored elements.
- End.
Example 1:
#include<iostream>
using namespace std;
int main() {
int a [2][2][3][2];
cout << "Enter the elements of array: \n";
for(int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
for(int k = 0; k < 3; ++k ) {
for(int l = 0; l < 2; ++l ) {
cin >> a[i][j][k][l];
}
}
}
}
cout<<"\n array elements are stored as:"<<endl;
for(int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
for(int k = 0; k < 3; ++k) {
for(int l = 0; l < 2; ++l) {
cout << "a[" << i << "][" << j << "][" << k << "] [" <<l<<"]= " << a[i][j][k][l] << endl;
}
}
}
}
return 0;
}
Output:
![4-Dimensional Array in C/C++](https://static.tutorialandexample.com/cpp/4-dimensional-array-in-c-or-cpp2.png)
![4-Dimensional Array in C/C++](https://static.tutorialandexample.com/cpp/4-dimensional-array-in-c-or-cpp3.png)
Example 2:
//Code for printing the values which is stored by user
#include <stdio.h>
int main()
{
int test[2][3][2];
printf("Enter 12 values: \n");
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 3; ++j)
{
for (int k = 0; k < 2; ++k)
{
scanf("%d", &test[i][j][k]);
}
}
}
// putting values in print with the correct index
printf("\nDisplaying values:\n");
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 3; ++j)
{
{
printf("test[%d][%d][%d] = %d\n", i, j, k, test[i][j][k]);
}
}
}
return 0;
}
Output:
![4-Dimensional Array in C/C++](https://static.tutorialandexample.com/cpp/4-dimensional-array-in-c-or-cpp4.png)
![4-Dimensional Array in C/C++](https://static.tutorialandexample.com/cpp/4-dimensional-array-in-c-or-cpp5.png)
Inserting arrays
#include<stdio.h>
int i,j,k; //nested loops variables
int main ()
{
int arr[2][3][3]; //declaration of an arrays
printf("enter the values in the array: \n");
for(i=1;i<=2;i++) //representations of blocks
{
for(j=1;j<=3;j++) //representations of rows
{
for(k=1;k<=3;k++) //representation of columns
{
printf("the value at arr[%d][%d][%d]: ",i,j,k);
scanf("%d",&arr[i][j][k]);
}
}
}
printf("printing the values in array: \n");
for(i=1;i<=2;i++)
{
for(j=1;j<=3;j++)
{
for(k=1;k<=3;k++)
{
printf("%d ",arr[i][j][k]);
if(k==3)
{
printf("\n");
}
}
}
printf("\n");
}
return 0;
}
Output:
![4-Dimensional Array in C/C++](https://static.tutorialandexample.com/cpp/4-dimensional-array-in-c-or-cpp6.png)
![4-Dimensional Array in C/C++](https://static.tutorialandexample.com/cpp/4-dimensional-array-in-c-or-cpp7.png)