A multidimensional array in C++ is an array that has more than one dimension. The most common form of multidimensional arrays are two-dimensional arrays, but you can have arrays with more dimensions (3D, 4D, etc.).
Multidimensional arrays can be described as “arrays of arrays”. For example, a bidimensional array can be imagined as a bidimensional table made of elements, all of them of a same uniform data type.
Two-Dimensional Array (2D Array)
A two-dimensional array can be thought of as an array of arrays. It’s often used to represent tables, grids, or matrices.
Syntax:
type arrayName[rowSize][columnSize];
Explanation:
- rowSize represents the number of rows
- columnSize represents the number of columns.
Example:
#include <iostream>
using namespace std;
int main() {
// Declare a 2D array with 2 rows and 3 columns
int arr[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
// Accessing and printing elements of the 2D array
for (int i = 0; i < 2; i++) { // Loop through rows
for (int j = 0; j < 3; j++) { // Loop through columns
cout << arr[i][j] << " ";
}
cout << endl; // New line after each row
}
return 0;
}
Explanation:
- int arr[2][3]: This declares a 2D array with 2 rows and 3 columns.
- arr[i][j]: This accesses the element at the i-th row and j-th column.
Output:
4 5 6
C++ Multidimensional Arrays – Interview Questions
Q 1: What is a multidimensional array?
Ans: An array with more than one dimension, like 2D or 3D arrays.
Q 2: How do you declare a 2D array?
Ans: int arr[3][3];
Q 3: How are elements accessed in 2D arrays?
Ans: Using row and column index: arr[i][j].
Q 4: Where are multidimensional arrays stored?
Ans: In contiguous memory (row-major order).
Q 5: What is a common use case?
Ans: Representing matrices or tables.
C++ Multidimensional Arrays – Objective Questions (MCQs)
Q1. Which of the following correctly declares a 2D array in C++?
Q2. How many elements does this array contain?
int arr[3][4];
Q3. How can you access the element in 2nd row and 3rd column?
Q4. Which of the following correctly initializes a 2D array?
Q5. What will be the output of the following code?
int arr[2][2] = {{5, 6}, {7, 8}};
cout << arr[1][0];