C++ Program to Multiply Two Matrices

18/06/2024 0 By indiafreenotes

Multiplying two matrices involves computing the dot product of rows from the first matrix with columns of the second matrix to get the elements of the resulting matrix.

Problem Statement

Given two matrices, compute their product. The number of columns in the first matrix must be equal to the number of rows in the second matrix for the multiplication to be defined.

Approach

  • Initialization:

Create a result matrix with dimensions corresponding to the number of rows of the first matrix and the number of columns of the second matrix.

  • Multiplication:

For each element in the result matrix, compute the sum of the products of corresponding elements from the rows of the first matrix and columns of the second matrix.

Example

For two matrices:

Matrix A:

1 2

3 4

5 6

Matrix B:

7  8  9

10 11 12

The resulting matrix C will be:

Matrix C:

(1*7 + 2*10)  (1*8 + 2*11)  (1*9 + 2*12)

(3*7 + 4*10)  (3*8 + 4*11)  (3*9 + 4*12)

(5*7 + 6*10)  (5*8 + 6*11)  (5*9 + 6*12)

Matrix C:

27  30  33

61  68  75

95  106 117

C++ Code Implementation

Here’s a C++ program to multiply two matrices:

#include <iostream>

#include <vector>

using namespace std;

// Function to multiply two matrices

vector<vector<int>> multiplyMatrices(const vector<vector<int>>& mat1, const vector<vector<int>>& mat2) {

    int rows1 = mat1.size();

    int cols1 = mat1[0].size();

    int cols2 = mat2[0].size();

   

    // Initialize result matrix with zeroes

    vector<vector<int>> result(rows1, vector<int>(cols2, 0));

   

    // Matrix multiplication

    for (int i = 0; i < rows1; ++i) {

        for (int j = 0; j < cols2; ++j) {

            for (int k = 0; k < cols1; ++k) {

                result[i][j] += mat1[i][k] * mat2[k][j];

            }

        }

    }

   

    return result;

}

// Function to print a matrix

void printMatrix(const vector<vector<int>>& matrix) {

    for (const auto& row : matrix) {

        for (const auto& elem : row) {

            cout << elem << ” “;

        }

        cout << endl;

    }

}

int main() {

    // Initializing two matrices

    vector<vector<int>> mat1 = {

        {1, 2},

        {3, 4},

        {5, 6}

    };

    vector<vector<int>> mat2 = {

        {7,  8,  9},

        {10, 11, 12}

    };

    cout << “Matrix A:” << endl;

    printMatrix(mat1);

    cout << “Matrix B:” << endl;

    printMatrix(mat2);

    // Multiplying the matrices

    vector<vector<int>> result = multiplyMatrices(mat1, mat2);

    cout << “Resultant Matrix (A * B):” << endl;

    printMatrix(result);

    return 0;

}

Explanation

  1. Function multiplyMatrices:

    • Parameters: Takes two 2D vectors (mat1 and mat2).
    • Initialization: Creates a result matrix filled with zeros, with dimensions (rows1, cols2), where rows1 is the number of rows in mat1 and cols2 is the number of columns in mat2.
    • Matrix Multiplication: Nested loops are used to iterate through the elements of the matrices and compute the dot products for the resulting matrix.
    • Return Value: Returns the resulting matrix after multiplication.
  2. Function printMatrix:

    • Prints the matrix in a formatted way for better visualization.
  3. Main Function:

    • Initializes two sample matrices.
    • Prints the matrices.
    • Calls the multiplyMatrices function to multiply the two matrices.
    • Prints the resulting matrix.