C++ program for matrix multiplication

In this article, you will learn a C++ program for matrix multiplication using arrays. We can multiply two matrices if the number of columns of the first matrix is equal to the number of rows of the second matrix.

Example 1: Matrix multiplication in C++

This program asks the user to enter the matrix size, rows, and column elements of both matrices. If the first matrix column size and second matrix rows size are not the same it displays the error message “multiplication is not possible” otherwise it displays the result on the console screen after multiplication.

//C++ program for matrix multiplication
#include <iostream>
using namespace std;

int main()
{
    int row1, col1, row2, col2, firstMatrix[10][10], secMatrix[10][10], result[10][10], i, j, k;

    cout << "Enter total number of rows for first Matrix: ";
    cin >> row1;
    cout << "Enter total number of columns for first Matrix: ";
    cin >>col1;
    
	cout << "Enter total number of rows for second Matrix: ";
    cin >> row2;
    cout << "Enter total number of columns for second Matrix: ";
    cin >> col2;

    while (col1!=row2)
    {
        cout << "Multiplication is not possible";

        cout << "Enter total number of rows for first Matrix: ";
        cin >> row1;
        cout << "Enter total number of columns for first Matrix: ";
        cin >>col1;
    
	    cout << "Enter total number of rows for second Matrix: ";
        cin >> row2;
        cout << "Enter total number of columns for second Matrix: ";
        cin >> col2;
    }

    cout << endl << "Enter elements for first matrix:" << endl;
    for(i = 0; i < row1; ++i)
        for(j = 0; j < col1; ++j)
        {
            cout << "Enter element for first matrix possition " << i + 1 << j + 1 << " : ";
            cin >> firstMatrix[i][j];
        }

    cout << endl << "Enter elements for second matrix:" << endl;
    for(i = 0; i < row2; ++i)
        for(j = 0; j < col2; ++j)
        {
            cout << "Enter element for second matrix possition " << i + 1 << j + 1 << " : ";
            cin >> secMatrix[i][j];
        }

    for(i = 0; i < row1; ++i)
        for(j = 0; j < col2; ++j)
        {
            result[i][j]=0;
        }

    for(i = 0; i < row1; ++i)
        for(j = 0; j < col2; ++j)
            for(k = 0; k < col1; ++k)
            {
                result[i][j] += firstMatrix[i][k] * secMatrix[k][j];
            }

    cout << endl << "Resultant matrix after multiplication " << endl;
    for(i = 0; i < row1; ++i)
    for(j = 0; j < col2; ++j)
    {
        cout << " " << result[i][j];
        if(j == col2-1)
            cout << endl;
    }

    return 0;
}

Output

Enter total number of rows for first Matrix: 2
Enter total number of columns for first Matrix: 2
Enter total number of rows for second Matrix: 2
Enter total number of columns for second Matrix: 2

Enter elements for first matrix:
Enter element for first matrix possition 11 : 4
Enter element for first matrix possition 12 : 5
Enter element for first matrix possition 21 : 2
Enter element for first matrix possition 22 : 7

Enter elements for second matrix:
Enter element for second matrix possition 11 : 6
Enter element for second matrix possition 12 : 4
Enter element for second matrix possition 21 : 3
Enter element for second matrix possition 22 : 6

Resultant matrix after multiplication
 39 46
 33 50

Recommended Articles

C++ program to find Transpose of matrix using array

C++ program for the Addition of two matrices

Scroll to Top