The Wayback Machine - https://web.archive.org/web/20220617162640/https://www.geeksforgeeks.org/how-to-iterate-a-multidimensional-array/
Skip to content
Related Articles

Related Articles

How to iterate a Multidimensional Array?

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 17 Jun, 2022

Multidimensional arrays are arrays that have more than one dimension. For example, a simple array is a 1-D array, a matrix is a 2-D array, and a cube or cuboid is a 3-D array but how to visualize arrays with more than 3 dimensions, and how to iterate over elements of these arrays? It is simple, just think of any multidimensional array as a collection of arrays of lower dimensions.

n-D array = Collection of (n-1)D arrays 

For example, a matrix or 2-D array is a collection of 1-D arrays.

2D Array is a collection of 1D Arrays

2D Array is a collection of 1D Arrays

Similarly, you can visualize 3-D arrays and other multidimensional arrays.

How to iterate over elements of a Multidimensional array? 

It can be observed that only a 1-D array contains elements and a multidimensional array contains smaller dimension arrays.

  •  Hence first iterate over the smaller dimension array and iterate over the 1-D array inside it. 
  • Doing this for the whole multidimensional array will iterate over all elements of the multidimensional array.

Example 1: Iterating over a 2-D array

C++




#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // Considering 2-D array having 3 rows and 3 columns
    int n = 3;
    int m = 3;
    int arr[][3]
        = { { 3, 2, 7 }, { 2, 6, 8 }, { 5, 1, 9 } };
  
    // Iterating over all 1-D arrays in 2-D array
    for (int i = 0; i < n; i++) {
  
        // Printing all elements in ith 1-D array
        for (int j = 0; j < m; j++) {
  
            // Printing jth element of ith row
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}

Output

3 2 7 
2 6 8 
5 1 9 

Example 2: Iterating over a 3D array

C++




#include <bits/stdc++.h>
using namespace std;
  
// Driver code
int main()
{
    // Consider 3-D array of dimensions 2*2*2
    int x = 2, y = 2, z = 2;
    int arr[][3][2] = { { { 1, 2 }, { 3, 4 } },
                        { { 5, 6 }, { 7, 8 } } };
  
    // Iterating over each 2-D array in 3-D array
    for (int i = 0; i < x; i++) {
        cout << "Inside " << i + 1
             << " 2D array in 3-D array" << endl;
  
        // Iterating over each 1-D array
        for (int j = 0; j < y; j++) {
            cout << "Inside " << j + 1
                 << " 1D array of the 2-D array" << endl;
  
            // Iterating over each element in 1-D array
            for (int k = 0; k < z; k++) {
                cout << arr[i][j][k] << " ";
            }
            cout << endl;
        }
        cout << endl;
    }
    return 0;
}

Output

Inside 1 2D array in 3-D array
Inside 1 1D array of the 2-D array
1 2 
Inside 2 1D array of the 2-D array
3 4 

Inside 2 2D array in 3-D array
Inside 1 1D array of the 2-D array
5 6 
Inside 2 1D array of the 2-D array
7 8 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!