4 Dimensional Array in C/C++
Last Updated :
14 Jun, 2022
Prerequisite :Array in C/C++, More on array A four-dimensional (4D) array is an array of array of arrays of arrays or in other words 4D array is a array of 3D array. More dimensions in an array means more data be held, but also means greater difficulty in managing and understanding arrays. Declaration of a Multidimensional Array in C: Syntax:
data_type array_name[i1][i2][i3][i4]………[in];
where each i is a dimension, and in is the size of final dimension.
Examples: 1. int student[4][5][6][7]; int designates the array type integer. student is the name of our 4D array. Our array can hold 840 integer-type elements. This number is reached by multiplying the value of each dimension. In this case: 4x5x6x7=840. 2. float country[5][6][5][6][5]; Array country is a five-dimensional array. It can hold 4500 floating-point elements (5x6x5x6x5=4500). Program :
C++
// C++ Program to input 4D Matrix and print it.
#include <iostream>
using namespace std;
int main()
{
// variable declaration used for indexes
int i, j, k, l, size;
// Array declaration
int a[2][2][2][2];
// size of array
size = 2;
// elements 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;
// Printing the values
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
<< "] :- " << a[i][j][k][l];
cout << "\n";
}
}
}
}
return 0;
}
// This code is contributed by sarajadhav12052009
C
// C Program to input 4D Matrix and print it.
#include <stdio.h>
int main(void)
{
// variable declaration used for indexes
int i, j, k, l, size;
// Array declaration
int a[2][2][2][2];
// size of array
size = 2;
// elements 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;
// Printing the values
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
for (k = 0; k < size; k++) {
for (l = 0; l < size; l++) {
printf("Value of a[%d][%d][%d][%d] :- %d ",
i, j, k, l, a[i][j][k][l]);
printf("\n");
}
}
}
}
return (0);
}
Output:Value of a[0][0][0][0] :- 5
Value of a[0][0][0][1] :- 3
Value of a[0][0][1][0] :- 5
Value of a[0][0][1][1] :- 3
Value of a[0][1][0][0] :- 6
Value of a[0][1][0][1] :- 7
Value of a[0][1][1][0] :- 6
Value of a[0][1][1][1] :- 7
Value of a[1][0][0][0] :- 8
Value of a[1][0][0][1] :- 9
Value of a[1][0][1][0] :- 8
Value of a[1][0][1][1] :- 9
Value of a[1][1][0][0] :- 9
Value of a[1][1][0][1] :- 7
Value of a[1][1][1][0] :- 9
Value of a[1][1][1][1] :- 7
Use: An 4D array can be used to store a collection of data, for example we input 3 coordinates & 1 time, i.e., x, y, z, t and we want to check whether there is collision between two vehicles or not.
Similar Reads
One Dimensional Arrays in C In C, an array is a collection of elements of the same type stored in contiguous memory locations. This organization allows efficient access to elements using their index. Arrays can also be of different types depending upon the direction/dimension they can store the elements. It can be 1D, 2D, 3D,
5 min read
C++ Multidimensional Array A multidimensional array is an array with more than one dimension. It means that it can grow in different directions i.e. instead of changing the length only, it can also change in width, depth or more.Syntax of Multidimensional ArraysC++data_type array_name[s1][s2]...[sn];where s1, s2,â¦, sn are the
8 min read
Dynamic Array in C Array in C is static in nature, so its size should be known at compile time and we can't change the size of the array after its declaration. Due to this, we may encounter situations where our array doesn't have enough space left for required elements or we allotted more than the required memory lead
9 min read
Array class in C++ The introduction of array class from C++11 has offered a better alternative for C-style arrays. The advantages of array class over C-style array are :- Array classes knows its own size, whereas C-style arrays lack this property. So when passing to functions, we don't need to pass size of Array as a
6 min read
How to declare a Two Dimensional Array of pointers in C? A Two Dimensional array of pointers is an array that has variables of pointer type. This means that the variables stored in the 2D array are such that each variable points to a particular address of some other element. How to create a 2D array of pointers: A 2D array of pointers can be created follo
3 min read