C++ Multi-Dimensional Arrays

Introduction

Multi-dimensional arrays in C++ are arrays of arrays. They allow you to store data in a grid-like format, such as matrices or tables. The most common type of multi-dimensional array is the two-dimensional array, which is often used to represent matrices. Understanding how to use multi-dimensional arrays is essential for handling complex data structures and performing operations on tabular data.

Declaring and Initializing Multi-Dimensional Arrays

Syntax for Declaring a Two-Dimensional Array

dataType arrayName[rows][columns];

Example: Declaring and Initializing a Two-Dimensional Array

#include <iostream>
using namespace std;

int main() {
    int matrix[3][3]; // Declares a 3x3 matrix

    // Initializing the matrix
    matrix[0][0] = 1; matrix[0][1] = 2; matrix[0][2] = 3;
    matrix[1][0] = 4; matrix[1][1] = 5; matrix[1][2] = 6;
    matrix[2][0] = 7; matrix[2][1] = 8; matrix[2][2] = 9;

    // Accessing and printing the matrix elements
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            cout << "matrix[" << i << "][" << j << "] = " << matrix[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}

Output

matrix[0][0] = 1 matrix[0][1] = 2 matrix[0][2] = 3
matrix[1][0] = 4 matrix[1][1] = 5 matrix[1][2] = 6
matrix[2][0] = 7 matrix[2][1] = 8 matrix[2][2] = 9

Explanation

  • int matrix[3][3]; declares a two-dimensional array named matrix with 3 rows and 3 columns.
  • Each element of the array is accessed and initialized using row and column indices.
  • Nested for loops are used to iterate through the matrix and print each element.

Initializing Arrays at Declaration

You can also initialize a multi-dimensional array at the time of declaration.

Example: Initializing a Two-Dimensional Array at Declaration

#include <iostream>
using namespace std;

int main() {
    int matrix[3][3] = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };

    // Accessing and printing the matrix elements
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            cout << "matrix[" << i << "][" << j << "] = " << matrix[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}

Output

matrix[0][0] = 1 matrix[0][1] = 2 matrix[0][2] = 3
matrix[1][0] = 4 matrix[1][1] = 5 matrix[1][2] = 6
matrix[2][0] = 7 matrix[2][1] = 8 matrix[2][2] = 9

Explanation

  • int matrix[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; declares and initializes the matrix in a single line.

Accessing Elements of Multi-Dimensional Arrays

Elements in a multi-dimensional array are accessed using row and column indices.

Example: Accessing and Modifying Elements

#include <iostream>
using namespace std;

int main() {
    int matrix[2][2] = {
        {1, 2},
        {3, 4}
    };

    // Accessing elements
    cout << "matrix[0][0] = " << matrix[0][0] << endl;
    cout << "matrix[1][1] = " << matrix[1][1] << endl;

    // Modifying elements
    matrix[0][1] = 5;
    matrix[1][0] = 6;

    // Printing modified matrix
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            cout << "matrix[" << i << "][" << j << "] = " << matrix[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}

Output

matrix[0][0] = 1
matrix[1][1] = 4
matrix[0][0] = 1 matrix[0][1] = 5
matrix[1][0] = 6 matrix[1][1] = 4

Explanation

  • The program initializes a 2×2 matrix and prints specific elements.
  • It modifies elements of the matrix and then prints the modified matrix.

Example Programs

Example 1: Matrix Addition

This example demonstrates adding two matrices and storing the result in a third matrix.

#include <iostream>
using namespace std;

int main() {
    int matrix1[2][2] = {
        {1, 2},
        {3, 4}
    };

    int matrix2[2][2] = {
        {5, 6},
        {7, 8}
    };

    int result[2][2];

    // Adding the matrices
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            result[i][j] = matrix1[i][j] + matrix2[i][j];
        }
    }

    // Printing the result
    cout << "Result of matrix addition:" << endl;
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            cout << result[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}

Output

Result of matrix addition:
6 8
10 12

Explanation

  • The program initializes two 2×2 matrices, matrix1 and matrix2.
  • It adds corresponding elements from matrix1 and matrix2 and stores the result in the result matrix.
  • The result matrix is printed.

Example 2: Matrix Multiplication

This example demonstrates multiplying two matrices and storing the result in a third matrix.

#include <iostream>
using namespace std;

int main() {
    int matrix1[2][2] = {
        {1, 2},
        {3, 4}
    };

    int matrix2[2][2] = {
        {5, 6},
        {7, 8}
    };

    int result[2][2] = {0}; // Initialize result matrix to zero

    // Multiplying the matrices
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            for (int k = 0; k < 2; k++) {
                result[i][j] += matrix1[i][k] * matrix2[k][j];
            }
        }
    }

    // Printing the result
    cout << "Result of matrix multiplication:" << endl;
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            cout << result[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}

Output

Result of matrix multiplication:
19 22
43 50

Explanation

  • The program initializes two 2×2 matrices, matrix1 and matrix2.
  • It multiplies the matrices using nested loops and stores the result in the result matrix.
  • The result matrix is printed.

Example 3: Transposing a Matrix

This example demonstrates transposing a matrix.

#include <iostream>
using namespace std;

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

    int transpose[3][2]; // Transposed matrix

    // Transposing the matrix
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            transpose[j][i] = matrix[i][j];
        }
    }

    // Printing the transposed matrix
    cout << "Transposed matrix:" << endl;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 2; j++) {
            cout << transpose[i][j] << " ";
        }
        cout << endl;
    }

    return 0

;
}

Output

Transposed matrix:
1 4
2 5
3 6

Explanation

  • The program initializes a 2×3 matrix and declares a 3×2 matrix for the transpose.
  • It transposes the matrix by swapping rows with columns.
  • The transposed matrix is printed.

Conclusion

Multi-dimensional arrays in C++ provide a way to store and manage collections of data in a grid-like format. This chapter covered how to declare, initialize, and access multi-dimensional arrays, focusing on two-dimensional arrays. It also included example programs to demonstrate matrix addition, matrix multiplication, and matrix transposition. Understanding how to use multi-dimensional arrays effectively will help you manage and process complex data structures in your programs. In the next chapter, we will explore C++ strings.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top