Home > Backend Development > C++ > In C/C++, 4-dimensional array

In C/C++, 4-dimensional array

王林
Release: 2023-09-01 23:57:06
forward
609 people have browsed it

In C/C++, 4-dimensional array

A 4-dimensional array is an array composed of 3-dimensional arrays.

Algorithm

Begin.
   Declare the variables.
   Declare the array elements.
   Take the no of elements as input.
   Take the elements as input.
   Print the elements stored in array.
End.
Copy after login

This is an example of a 4D array.

#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<<"\narray 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;
}
Copy after login

Output

Enter the elements of array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

array elements are stored as:
a[0][0][0] [0]= 1
a[0][0][0] [1]= 2
a[0][0][1] [0]= 3
a[0][0][1] [1]= 4
a[0][0][2] [0]= 5
a[0][0][2] [1]= 6
a[0][1][0] [0]= 7
a[0][1][0] [1]= 8
a[0][1][1] [0]= 9
a[0][1][1] [1]= 10
a[0][1][2] [0]= 11
a[0][1][2] [1]= 12
a[1][0][0] [0]= 13
a[1][0][0] [1]= 14
a[1][0][1] [0]= 15
a[1][0][1] [1]= 16
a[1][0][2] [0]= 17
a[1][0][2] [1]= 18
a[1][1][0] [0]= 19
a[1][1][0] [1]= 20
a[1][1][1] [0]= 21
a[1][1][1] [1]= 22
a[1][1][2] [0]= 23
a[1][1][2] [1]= 24
Copy after login

The above is the detailed content of In C/C++, 4-dimensional array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template