Multidimensional Arrays in C++

Multidimensional Arrays in C++

21 May 2024
Intermediate
3.2K Views
20 min read
Learn via Video Course & by Doing Hands-on Labs

Free C++ Course Online with Certificate [Full Course]

Multidimensional Arrays in C++: An Overview

We already saw what are arrays in C++. Now you might have become familiar with the basic concepts of array in C++. Let us now begin with the Multidimensional Arrays in C++ in our C++ tutorial. To certify your skills, you can also look into our C++ Certification Course.

Multidimensional Arrays in C++

Arrays can have any number of dimensions. In C++ programming, you can create an array of arrays.

Syntax

To create a multidimensional array in C++, you need to specify the number of dimensions and the size of each dimension.
dataType arrayName[size1][size2]...[sizeN];

Example

int arr[5][4]; 

Here, an integer type 2D array, arris declared with 5 rows and 4 columns thus a total of 20 integer elements.

In the same manner, as above, a 3D array can be declared.

Example

int arr[2][4][3];

The array arr can store 24 integer elements.

Read More - C++ Interview Interview Questions and Answers

How to initialize a multidimensional array in C++?

  • 2D array

It is a matrix (a table of rows and columns).
int arr[2][3] = {{1, 3, 0}, {-1, 5, 9}};
 
int arr[][3] = {{1, 3, 0}, {-1, 5, 9}};
 
int arr[2][3] = {1, 3, 0, -1, 5, 9};

  • 3D array
  • The initialization is similar to a 2D array.
    int arr[2][3][4] = {
     {{3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2}},
     {{13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9}}
     };
    

    How to access the elements of a multi-dimensional array in C++?

    • 2D array

    In this type of array, you must specify the index number of both the row and column.

    Example

     #include <iostream>
    using namespace std;
    int main() {
     int arr[2][3] = {{5, 6, 7}, {3, 6, 8}};
     cout << arr[0][2];
     return 0;
    }
    

    The coutstatement in the above code in C++ Compiler accesses the value of the element in the first row 0 and third column 2 of the array, arr.

    Output

    7
    
    • 3D array

    Example

    #include <iostream>
    using namespace std;
    int main() {
     int arr[2][3][4] = {
     {{3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2}},
     {{13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9}}
     };
     cout << arr[0][2][3];
     return 0;
    }
    

    The cout statement in the above code accesses the value of the element in the first row 0, the third column 2, and the fourth dimension 3of the array, arr.

    Output

    2
    

    How to change the elements of a multidimensional array in C++?

    To change the value of a specific element, refer to the index number in each dimension.

    • 2D array

    Example

    #include <iostream>
    using namespace std;
    int main() {
     int matrix[2][3] = {{5, 6, 7}, {3, 6, 8}};
     matrix[0][2] = 9;
     cout << matrix[0][2]; // Now outputs 9
     return 0;
    }
    

    Output

    9
    

    How to traverse a multidimensional array in C++?

    Traversing a multidimensional array can be done in two ways

    • 2D array

    • using for loop

      Example

      #include <iostream>
      using namespace std;
      int main() {
       int arr[2][3] = {{5, 6, 7}, {3, 6, 8}};
       int i, j;
      
       for (i = 0; i < 2; i++) {
       for (j = 0; j < 3; j++) {
       cout << arr[i][j] << endl;
       }
       }
       return 0;
      }
      

      Output

      5
      6
      7
      3
      6
      8
      
    • using for-each loop

      Example

       #include <iostream>
      using namespace std;
      
      int main() {
       int arr[2][3] = {{5, 6, 7}, {3, 6, 8}};
      
       for (const int (&row)[3] : arr) {
       for (int element : row) {
       cout << element << endl;
       }
       }
       return 0;
      }
      

      In the above C++ code, we use a range-based for loop to iterate through each row in the 2D array. Again, for each row, we use another range-based for loop to iterate through the elements within that row. const int (&row)[3] species that row is a reference to an array of integers with a size of 3.

      Output

      5
      6
      7
      3
      6
      8
      

    Input and Output Multidimensional Array Elements in C++

    We can use the cinfunction to take inputs of an array from the user.

    • 2D array

    Example

    #include <iostream>
    using namespace std;
    int main() {
     int marks[2][3];
    
     // Prompt user to enter grades
     for (int i = 0; i < 2; i++) {
     cout << "Enter the marks for student " << i + 1 << ":\n";
     for (int j = 0; j < 3; j++) {
     cout << "Subject " << j + 1 << ": ";
     cin >> marks[i][j];
     }
     }
    
     cout << "The marks entered are:\n";
    
     // Display entered grades
     for (int i = 0; i < 2; i++) {
     cout << "Student " << i + 1 << ": ";
     for (int j = 0; j < 3; j++) {
     cout << "Subject " << j + 1 << ": " << marks[i][j] << endl;
     }
     }
     return 0;
    }
    

    The above program is to print the marks of two students in 3 subjects. The first for loop indicates the student number. The 2nd for loop is used to take the marks of the 3 subjects for the student in the first for loop from the user. In the same way, the two for loops work for printing the output

    Output

    Enter the marks for student 1:
    Subject 1: 95
    Subject 2: 88
    Subject 3: 92
    Enter the marks for student 2:
    Subject 1: 78
    Subject 2: 85
    Subject 3: 90
    The marks entered are:
    Student 1: Subject 1: 95
    Subject 2: 88
    Subject 3: 92
    Student 2: Subject 1: 78
    Subject 2: 85
    Subject 3: 90
    

    • 3D array

    Example in C++ Online Editor

     #include <iostream>
    using namespace std;
    int main() {
     int arr[2][3][2];
    
     cout << "Enter the values as given by the user: \n";
    
     for (int i = 0; i < 2; ++i) {
     for (int j = 0; j < 3; ++j) {
     for (int k = 0; k < 2; ++k) {
     cin >> arr[i][j][k];
     }
     }
     }
    
     // Printing values with the proper index.
    
     cout << "\nDisplaying values:\n";
     for (int i = 0; i < 2; ++i) {
     for (int j = 0; j < 3; ++j) {
     for (int k = 0; k < 2; ++k) {
     cout << "arr[" << i << "][" << j << "][" << k << "] = " << arr[i][j][k] << endl;
     }
     }
     }
     return 0;
    }
    

    Output

    Enter the values as given by the user:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
    Displaying values:
    arr[0][0][0] = 1
    arr[0][0][1] = 2
    arr[0][1][0] = 3
    arr[0][1][1] = 4
    arr[0][2][0] = 5
    arr[0][2][1] = 6
    arr[1][0][0] = 7
    arr[1][0][1] = 8
    arr[1][1][0] = 9
    arr[1][1][1] = 10
    arr[1][2][0] = 11
    arr[1][2][1] = 12
    
    Summary
    This was all about multidimensional arrays in C++. With this, the topic of arrays in C++ gets completed. Now, the only thing is to implement whatever learned till now. You must know where and what kind of array is required. If you're interested in more tips and guidance, you may also consider a C++ Certification, which can validate your skills and enhance your credibility in the field.

    Share Article

    Live Classes Schedule

    Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
    ASP.NET Core Certification TrainingNov 08MON, TUE, WED, THU, FRI
    Filling Fast
    07:00AM to 08:30AM (IST)
    Get Details
    Azure Developer Certification TrainingNov 09SAT, SUN
    Filling Fast
    08:30PM to 10:30PM (IST)
    Get Details
    .NET Solution Architect Certification TrainingNov 09SAT, SUN
    Filling Fast
    05:30PM to 07:30PM (IST)
    Get Details
    .NET Microservices Certification TrainingNov 09SAT, SUN
    Filling Fast
    05:30PM to 07:30PM (IST)
    Get Details
    Microsoft Azure Cloud Architect with AINov 10SAT, SUN
    Filling Fast
    07:00AM to 09:00AM (IST)
    Get Details
    ASP.NET Core Certification TrainingNov 10SAT, SUN
    Filling Fast
    10:00AM to 12:00PM (IST)
    Get Details
    Angular Certification TrainingNov 17SAT, SUN
    Filling Fast
    06:00PM to 08:00PM (IST)
    Get Details
    Software Architecture and Design TrainingNov 24SAT, SUN
    Filling Fast
    10:00AM to 12:00PM (IST)
    Get Details
    .NET Solution Architect Certification TrainingNov 24SAT, SUN
    Filling Fast
    10:00AM to 12:00PM (IST)
    Get Details

    Can't find convenient schedule? Let us know

    About Author
    Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

    Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 9th time in a row (2016-2024). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
    Self-paced Membership
    • 24+ Video Courses
    • 825+ Hands-On Labs
    • 400+ Quick Notes
    • 125+ Skill Tests
    • 10+ Interview Q&A Courses
    • 10+ Real-world Projects
    • Career Coaching Sessions
    • Email Support
    Upto 60% OFF
    Know More
    Accept cookies & close this