Skip to content
Related Articles
Open in App
Not now

Related Articles

4 Dimensional Array in C/C++

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 14 Jun, 2022
Improve Article
Save Article

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.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!