C++ Program to Multiply Two Matrix Using Multi-dimensional Arrays

Updated on December 19, 2024
Multiply two matrix using multi-dimensional arrays header image

Introduction

Matrix multiplication is a crucial operation in various fields such as computer graphics, physics simulations, and machine learning. It involves calculating the dot product of rows and columns across two matrices. While the concept might seem straightforward, implementing it correctly in C++ requires a precise understanding of how multi-dimensional arrays work.

In this article, you will learn how to multiply two matrices using multi-dimensional arrays in C++. Through detailed examples, uncover the step-by-step approach to set up your matrices, perform the multiplication, and display the resultant matrix. This will enhance your ability to manipulate and process data efficiently in C++.

Defining the Matrices

Before multiplying two matrices, you need to establish their dimensions and ensure the number of columns in the first matrix matches the number of rows in the second. Let's start by defining two matrices.

Declare the Matrices

  1. Specify the dimensions for matrix A and matrix B.

  2. Declare two 2D arrays in C++ to represent these matrices.

    cpp
    const int ROWS_A = 2;
    const int COLS_A = 3;
    const int ROWS_B = 3;
    const int COLS_B = 2;
    int matrixA[ROWS_A][COLS_A] = {{1, 2, 3}, {4, 5, 6}};
    int matrixB[ROWS_B][COLS_B] = {{7, 8}, {9, 10}, {11, 12}};
    

    This code initializes matrix A of size 2x3 and matrix B of size 3x2 with predefined values. The multiplication of these matrices will result in a 2x2 matrix.

Initialize the Resultant Matrix

  1. Declare the resultant matrix with dimensions derived from the input matrices.

    cpp
    int resultMatrix[ROWS_A][COLS_B] = {0}; // Initialize all elements to 0
    

    The resultant matrix should have the same number of rows as matrix A and the same number of columns as matrix B. Initializing to zero is essential for accumulating sum during multiplication.

Performing Matrix Multiplication

Matrix multiplication involves multiplying corresponding rows and columns. The value at each position in the resultant matrix is obtained by summing up these products.

Loop through Each Element

  1. Implement nested loops to traverse rows of the first matrix, columns of the second matrix, and elements for multiplication.

    cpp
    for (int i = 0; i < ROWS_A; ++i) {
        for (int j = 0; j < COLS_B; ++j) {
            for (int k = 0; k < COLS_A; ++k) { // or ROWS_B, since COLS_A must equal ROWS_B for multiplication
                resultMatrix[i][j] += matrixA[i][k] * matrixB[k][j];
            }
        }
    }
    

    Here, the three nested loops correspond to:

    • i traverses the rows of the first matrix and rows of the result.
    • j traverses the columns of the second matrix and columns of the result.
    • k traverses the elements along the column of the first matrix and the row of the second matrix, performing the multiplication and accumulation.

Displaying the Result

After calculating the product, display the resultant matrix to verify the correctness of the multiplication.

  1. Use nested loops to print each element in the resultant matrix.

    cpp
    for (int i = 0; i < ROWS_A; ++i) {
        for (int j = 0; j < COLS_B; ++j) {
            std::cout << resultMatrix[i][j] << " ";
        }
        std::cout << std::endl;
    }
    

    These loops iterate over each element in the resultant matrix, printing them formatted as a matrix where each row is followed by a newline.

Conclusion

Matrix multiplication using multi-dimensional arrays in C++ is a foundational skill that bolsters your capability to implement algorithms requiring data structuring and transformation. By understanding how to set up multi-dimensional arrays, loop through them, and perform element-wise operations, you bolster various programming tasks, including algorithm design, data manipulation, and even advancing to more complex data structures. Always ensure your matrices dimensions align correctly for multiplication and take advantage of C++ features like constants and initialization to make your code robust and understandable.