C++ Program to Perform Matrix Multiplication



A matrix is a rectangular array of numbers arranged in rows and columns. To multiply two matrices, We multiply each row of the first matrix by each column of the second matrix and add the results to get a new matrix.

An example of the multiplication of two matrices is as follow:

multiplication of matrices

Steps for Matrix Multiplication

To multiply the matrices, we use pointers in C++, which means we directly access the elements through their memory addresses to read and calculate the values. Below are the steps we took:

  • We first define the number of rows and columns for the matrices.
  • Next, we create two 2D arrays, a and b, and initialize them with values to represent the two matrices.
  • Then, we use three loops:
  • The first goes through each row of the first matrix.
  • The second goes through each column of the second matrix.
  • The third multiplies corresponding elements and adds them up.
  • Put this sum in the result matrix and print the result matrix.

C++ Program to Perform Matrix Multiplication

Below is the complete C++ program where we multiply two matrices using pointers to access elements. It calculates the product by adding the multiplication of matching row and column:

#include <iostream>
using namespace std;

int main() {
    int product[10][10], r1 = 3, c1 = 3, r2 = 3, c2 = 3;
    int a[3][3] = { {2, 4, 1}, {2, 3, 9}, {3, 1, 8} };
    int b[3][3] = { {1, 2, 3}, {3, 6, 1}, {2, 4, 7} };

    // Checking if the matrices can be multiplied
    if (c1 != r2) {
        cout << "Error: Columns of first matrix must be equal to rows of second matrix." << endl;
        return 1;
    }
    // Printing the first matrix
    cout << "First matrix:" << endl;
    for (int i = 0; i < r1; ++i) {
        for (int j = 0; j < c1; ++j)
            cout << *(*(a + i) + j) << " ";
        cout << endl;
    }
    cout << endl;
    // Printing the second matrix
    cout << "Second matrix:" << endl;
    for (int i = 0; i < r2; ++i) {
        for (int j = 0; j < c2; ++j)
            cout << *(*(b + i) + j) << " ";
        cout << endl;
    }
    cout << endl;

    // Initialize all elements of product matrix to 0
    for (int i = 0; i < r1; ++i)
        for (int j = 0; j < c2; ++j)
            product[i][j] = 0;

    // Multiply matrices using pointer access
    for (int i = 0; i < r1; ++i) {
        for (int j = 0; j < c2; ++j) {
            int sum = 0;
            for (int k = 0; k < c1; ++k) {
                // Multiply and add elements from row of a and column of b
                sum += (*(*(a + i) + k)) * (*(*(b + k) + j));
            }
            product[i][j] = sum;
        }
    }
    // Printing the resulting product matrix
    cout << "Product matrix:" << endl;
    for (int i = 0; i < r1; ++i) {
        for (int j = 0; j < c2; ++j)
            cout << product[i][j] << " ";
        cout << endl;
    }
    return 0;
}

Below you will see the output of the above program which shows the multiplication of two matrices, you can even modify the matrices to check different matrices.

First matrix:
2 4 1 
2 3 9 
3 1 8 

Second matrix:
1 2 3 
3 6 1 
2 4 7 

Product matrix:
16 32 17 
29 58 72 
22 44 66 

Time Complexity: O(r1*c1*c2) because we use three nested loops.

Space Complexity: O(r1 * c2) because we store the product matrix.

Updated on: 2025-05-16T18:37:23+05:30

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements