Print Dimensions of Multidimensional Array in C++



A multidimensional array is an array with more than one dimension. It means that the array can grow in multiple directions, such as length, width, and height. In this article, we will learn how to print the dimensions of given multidimensional array in C++. First of all, let's understand the problem statement.

You are given a multidimensional array as input, and you need to print its dimensions such as the number of rows, number of columns, and so on. For example:

// Input multidimensional array
int arr[3][4] = {
    {1, 2, 3, 4},
    {5, 6, 7, 8},
    {9, 10, 11, 12}
};

// Output
Dimensions: 3 rows, 4 columns

// Explanation: The array has 3 rows and 4 columns.

Printing Dimensions of Multidimensional Array

To solve this problem, we have to consider two cases, one for static multidimensional arrays and another for dynamic multidimensional arrays. For static arrays we can directly use the sizeof operator to calculate the dimensions. But for dynamic arrays, the compiler does not store the size information, so we have to pass the dimensions as parameters.

Dimensions of Static Multidimensional Array

Static arrays are the arrays whose size is known at compile time. You can use the sizeof operator to check the size of the array and calculate the number of rows and columns. But, this method only works when you know the D (number of dimensions) of the array at compile time. If you are not sure about the number of dimensions, you can use a template with function overloading to handle multiple dimensions. Let's see an example for both of these cases.

Example 1: Using sizeof Operator

As discussed above, we can use the sizeof operator to calculate the dimensions of a static multidimensional array if we are sure about the number of dimensions. The code below shows how to do this:

#include <iostream>
using namespace std;

int main() {
    int arr[3][4]; // 3 rows, 4 columns

    int rows = sizeof(arr) / sizeof(arr[0]);
    int cols = sizeof(arr[0]) / sizeof(arr[0][0]);

    cout << "Rows: " << rows << endl;
    cout << "Columns: " << cols << endl;

    return 0;
}

The output of the above code will be:

Rows: 3
Columns: 4

Example 2: Using Function Overloading

Sometimes, you may not know the array you are checking is a 2D or 3D array. In such cases, you can define function overloading with templates to receive the array of varying dimensions. Learn about function overloading and templates to understand this program better.

#include <iostream>
using namespace std;

// 1D array overload
template <typename T, size_t N>
void printDimensions(T (&arr)[N]) {
    cout << "This is a 1D array." << endl;
    cout << "Size: " << N << endl;
}

// 2D array overload
template <typename T, size_t R, size_t C>
void printDimensions(T (&arr)[R][C]) {
    cout << "This is a 2D array." << endl;
    cout << "Rows: " << R << ", Columns: " << C << endl;
}

// 3D array overload
template <typename T, size_t X, size_t Y, size_t Z>
void printDimensions(T (&arr)[X][Y][Z]) {
    cout << "This is a 3D array." << endl;
    cout << "Depth: " << X << ", Rows: " << Y << ", Columns: " << Z << endl;
}

int main() {
    // 1D array
    int a1D[5]       = {1, 2, 3, 4, 5};
    // 2D array
    int a2D[2][3]    = {{1, 2, 3}, {4, 5, 6}};
    // 3D array
    int a3D[2][2][3] = {
        {{1, 2, 3}, {4, 5, 6}},
        {{7, 8, 9}, {10, 11, 12}}
    };

    cout << "Array 1:" << endl;
    printDimensions(a1D);

    cout << "\nArray 2:" << endl;
    printDimensions(a2D);

    cout << "\nArray 3:" << endl;
    printDimensions(a3D);

    return 0;
}

The output of the above code will be:

This is a 1D array.
Size: 5
This is a 2D array.
Rows: 2, Columns: 3
This is a 3D array.
Depth: 2, Rows: 2, Columns: 3

Dimensions of Dynamic Multidimensional Array

Dynamic arrays are the arrays that are allocated at runtime. So, the compiler does not store the size information of the array. To print the dimensions of a dynamic multidimensional array, you need to pass the dimensions as parameters to the function. Below is an example of how to do this:

Example: Using Function Parameters

While working with dynamic arrays, it is developers responsibility to keep track of the the size and dimension of the array. So, to print the dimensions of a dynamic multidimensional array, we can create a function that takes the number of rows and columns as parameters.

#include <iostream>
using namespace std;

void printDimensions(int rows, int cols) {
    cout << "Rows: " << rows << ", Columns: " << cols << endl;
}

int main() {
    int rows = 3, cols = 4;

    // Dynamically allocate a 2D array
    int **arr = new int*[rows];
    for (int i = 0; i < rows; i++) {
        arr[i] = new int[cols];
    }

    // Print dimensions
    printDimensions(rows, cols);

    // Deallocate memory
    for (int i = 0; i < rows; i++) {
        delete[] arr[i];
    }
    delete[] arr;

    return 0;
}

The output of the above code will be:

Rows: 3, Columns: 4
Farhan Muhamed
Farhan Muhamed

No Code Developer

Updated on: 2025-05-28T17:37:16+05:30

189 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements