C++ Program to Multiply Two Matrix


January 20, 2023, Learn eTutorial
1404

Multidimensional arrays in C++

In C++, we can make multidimensional arrays which means an array of some arrays. For example, int x[3][4], x is a multidimensional array with 3 rows and 4 columns, or we can call it a matrix, that can hold a maximum of 12 elements.

How to multiply two matrices?

For solving this C++ program, we need to know how to multiply two matrices. We can do the matrix multiplication only if the number of columns in the first matrix equals the number of rows in the second matrix. then the resulting matrix will have the same number of rows as the first matrix and the same columns as the second matrix.

How to multiply matrices using C++ program?

Here the user is asked to enter the number of rows r1 and number of columns c1 of the first matrix and the number of rows r2 and number of columns c2 of the 2nd matrix. Check whether column number in the first matrix matches the row number of the second matrix by using a while statement, While (c1 != r2 ).

If it is not equal, Then print an error message else, ask the user to enter the rows and columns for the first and second matrix. If the condition c1 = r2 gets satisfied then store elements of the first matrix.


Ask the user to enter elements of the first matrix. Read the elements into the first matrix using a nested for loop. Read the elements into the first array a[i][j]. Similarly read elements for the second matrix, b [i] [j].

Access the first element from the first column of matrix 'a' and the first element from the first row of matrix 'b'. multiply the elements and store the result in an array mul. Display the resultant matrix 

Algorithm

Step 1:  Call the header file iostream.

Step 2: Use the namespace std.

Step 3: Open the integer type main function; int main().

Step 4: Declare integer type variables r1 , c1, r2, c2, i, j, k; and array a[10][10], b[10] [10], mul[10][10];

Step 5: Ask the user to enter the number of rows and columns for the first matrix and second matrix.

Step 6: read the number into the variable r1, c1, and r2, c2 respectively;

Step 7: Check whether c1=r2 using a while loop. If not, then print an error message and repeat step 5.

Step 8: Ask the user to enter the  elements for the first matrix

Step 9: Read the element into the array a[10][10]

Step 10: Ask the user to enter the  elements for the second matrix

Step 11: Read the elements into the array b[10][10];

Step 12: initialize the elements of matrix mul to 0;

Step 13: Get the first element from the first row of the matrix a[10][10] and  the first element from the first column of the matrix b[10][10];

Step 14: Multiply both the elements and store the result within the array mul[10][10];

Step 15: Repeat the step 14 until a[r1][c1] and b[r2][c2]

Step 16: Display mul[10][10]

Step 17: exit.

C++ Source Code

                                          #include <iostream>
using namespace std;

int main()
{
    int a[10][10], b[10][10], mul[10][10], r1, c1, r2, c2, i, j, k;

    cout << "Enter rows and columns for first matrix: ";
    cin >> r1 >> c1;
    cout << "Enter rows and columns for second matrix: ";
    cin >> r2 >> c2;

    // If column of first matrix in not equal to row of second matrix,
    // ask the user to enter the size of matrix again.
    while (c1!=r2)
    {
        cout << "Error! column of first matrix not equal to row of second.";

        cout << "Enter rows and columns for first matrix: ";
        cin >> r1 >> c1;

        cout << "Enter rows and columns for second matrix: ";
        cin >> r2 >> c2;
    }

    // Storing elements of first matrix.
    cout << endl << "Enter elements of matrix 1:" << endl;
    for(i = 0; i < r1; ++i)
        for(j = 0; j < c1; ++j)
        {
            cout << "Enter element a" << i + 1 << j + 1 << " : ";
            cin >> a[i][j];
        }

    // Storing elements of second matrix.
    cout << endl << "Enter elements of matrix 2:" << endl;
    for(i = 0; i < r2; ++i)
        for(j = 0; j < c2; ++j)
        {
            cout << "Enter element b" << i + 1 << j + 1 << " : ";
            cin >> b[i][j];
        }

    // Initializing elements of matrix mul to 0.
    for(i = 0; i < r1; ++i)
        for(j = 0; j < c2; ++j)
        {
            mul[i][j]=0;
        }

    // Multiplying matrix a and b and storing in array mul.
    for(i = 0; i < r1; ++i)
        for(j = 0; j < c2; ++j)
            for(k = 0; k < c1; ++k)
            {
                mul[i][j] += a[i][k] * b[k][j];
            }

    // Displaying the multiplication of two matrix.
    cout << endl << "Output Matrix: " << endl;
    for(i = 0; i < r1; ++i)
    for(j = 0; j < c2; ++j)
    {
        cout << " " << mul[i][j];
        if(j == c2-1)
            cout << endl;
    }

    return 0;
}
                                      

OUTPUT

Enter rows and columns for first matrix: 2
3
Enter rows and columns for second matrix: 2
3
Error! column of first matrix not equal to row of second.Enter rows and columns for first matrix: 3
2
Enter rows and columns for second matrix: 2
3
Enter elements of matrix 1:
Enter element a11 : 3
Enter element a12 : 2
Enter element a21 : 5
Enter element a22 : 8
Enter element a31 : 4
Enter element a32 : 6
Enter elements of matrix 2:
Enter element b11 : 4
Enter element b12 : 2
Enter element b13 : 6
Enter element b21 : 3
Enter element b22 : 8
Enter element b23 : 4
Output Matrix: 
 18 22 26
 44 74 62
 34 56 48