C++ Program to Multiply Two Matrix

Here we discuss a C++ program to multiply two matrices.

How to multiply two matrices?

The product of two matrices will be defined if the number of columns in the first matrix is equal to the number of rows in the second matrix. If the product is defined, the resulting matrix will have the same number of rows as the first matrix and the same number of columns as the second matrix.

Multidimensional arrays in C++

In C++, we can create an array of an array, known as a multidimensional array. For example: int x[3][4];
Here, x is a two-dimensional array. It can hold a maximum of 12 elements.
We can think of this array as a table with 3 rows and each row has 4 columns 
Three-dimensional arrays also work similarly. For example:
float x[2][4][3];
This array x can hold a maximum of 24 elements.
We can find out the total number of elements in the array simply by multiplying its dimensions:
2 x 4 x 3 = 24

C++ program to multiply two matrices using multi-dimensional arrays.

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 the number of columns in the first matrix is equal to the number of rows in the second matrix. This can be done by using a while statement. 
While (c1 != r2 )
Then print an error message and 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. For loop can be used to do this. Initialize the variableto point the rows and j for point the columns.
Initialize the value of both and j to 0. The condition will be like i < r1 and j < c1. Update the value of bothand j by 1 for each iteration.
Read the elements into the first array  a[i][j]. 
Similarly read elemnts 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