C++ Beginner

cpp_012: Multidimensional Arrays in C++

codeaddict 2025. 2. 8. 15:09

Multidimensional arrays are an extension of one-dimensional arrays, allowing you to store data in a grid-like structure. They are particularly useful for representing matrices, tables, or any data that requires multiple dimensions. In this lesson, we’ll explore how to declare, initialize, and use multidimensional arrays in C++. We’ll also cover how to input data into arrays using cin and handle early loop exits.


1. What Are Multidimensional Arrays?

A multidimensional array is an array of arrays. For example, a 2D array can be thought of as a table with rows and columns, while a 3D array can be visualized as a cube with layers, rows, and columns.


2. Declaring and Initializing Multidimensional Arrays

To declare a 2D array in C++, use the following syntax:

data_type array_name[rows][columns];

Example:

int matrix[2][3] = {
    {1, 2, 3},
    {4, 5, 6}
};

For a 3D array:

int cube[2][3][4] = {
    {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12}
    },
    {
        {13, 14, 15, 16},
        {17, 18, 19, 20},
        {21, 22, 23, 24}
    }
};

3. Accessing Elements in Multidimensional Arrays

You can access elements using nested loops. For example, to print a 2D array:

#include <iostream>
using namespace std;

int main() {
    // Declaring and initializing a 2D array
    int matrix[2][3] = {
        {1, 2, 3},
        {4, 5, 6}
    };

    // Loop to traverse the 2D array and print its elements
    for (int i = 0; i < 2; i++) {   // Loop through rows
        for (int j = 0; j < 3; j++) {  // Loop through columns
            cout << matrix[i][j] << " ";  // Print each element
        }
        cout << endl;  // Print a new line after each row
    }

    return 0;  // Return 0 to indicate successful execution
}

4. Inputting Data into Arrays Using cin

You can use cin to populate arrays with user input. Here’s how:

For a 1D Array:

int arr[5];
cout << "Enter 5 elements: ";
for (int i = 0; i < 5; i++) {
    cin >> arr[i];
}

Complete code:

#include<iostream>
using namespace std;

int main() {
    int arr[5];  // Declare an array of size 5

    // Input values into the array
    cout << "Enter 5 integers: ";
    for (int i = 0; i < 5; i++) {
        cin >> arr[i];  // Take input for each element of the array
    }

    // Output the elements of the array
    cout << "Array elements: ";
    for (int i = 0; i < 5; i++) {
        cout << arr[i] << " ";  // Print each element followed by a space
    }
    cout << endl;  // Print a new line after the array

    return 0;  // Return 0 to indicate successful execution
}

For a 2D Array:

int matrix[2][3];
cout << "Enter 6 elements (2 rows x 3 columns): ";
for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 3; j++) {
        cin >> matrix[i][j];
    }
}

Complete code:

#include<iostream>
using namespace std;

int main() {
    // Declare a 2D array with 2 rows and 3 columns
    int matrix[2][3];

    // Input elements into the matrix
    cout << "Enter 6 elements (2 rows x 3 columns): ";
    for (int i = 0; i < 2; i++) {       // Loop through rows
        for (int j = 0; j < 3; j++) {   // Loop through columns
            cin >> matrix[i][j];  // Read input for each element
        }
    }

    // Output the elements of the matrix
    cout << "The matrix is:" << endl;
    for (int i = 0; i < 2; i++) {       // Loop through rows
        for (int j = 0; j < 3; j++) {   // Loop through columns
            cout << matrix[i][j] << " ";  // Print each element
        }
        cout << endl;  // Print a new line after each row
    }

    return 0;  // Return 0 to indicate successful execution
}