4 Dimensional Array in C/C++
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 wordes 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 Program to input 4D Matrix and print it. #include <stdio.h> 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++) { printf("Value of a[%d][%d][%d][%d] :- %d ", i, j, k, l, a[i][j][k][l]); printf("\n"); } } } } return 0; } |
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.
Recommended Posts:
- Difference between pointer to an array and array of pointers
- array::front() and array::back() in C++ STL
- array::crbegin() and array::crend() in C++ STL
- array::cbegin() and array::cend() in C++ STL
- array::fill() and array::swap() in C++ STL
- array::rbegin() and array::rend() in C++ STL
- What’s difference between “array” and “&array” for “int array[5]” ?
- array::begin() and array::end() in C++ STL
- Sum of an array using MPI
- STD::array in C++
- array::at() in C++ STL
- array get() function in C++ STL
- array at() function in C++ STL
- How to reverse an Array using STL in C++?
- How to sort an Array using STL in C++?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : vitkaodessit



