C++ Program to Multiply Two Matrices Using Multidimensional Arrays

In this program, the user is asked to enter the elements of the two matrices a and b. Then the elements of these two matrices are multiplied and saved it in third matrix. Finally, the result (third matrix) is printed on the screen.



Source Code
#include<iostream>
using namespace std;
int main()
{
    int a[3][3], b[3][3], c[3][3], i, j, k;

    cout<<"Enter 1st matrix\n";
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            cin>> a[i][j];
        }
    }
    cout<<"Enter 2nd matrix\n";
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            cin>> b[i][j];
        }
    }

    cout<<"\nMultiplication of A and B matrices is\n";
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            c[i][j] = 0;
            for(k=0; k<3; k++)
            {
                c[i][j] = c[i][j] + a[i][k] * b[k][j];
            }
            cout<< c[i][j] << " ";
        }
        cout<<"\n";
    }
    return 0;
}
Output
Enter 1st matrix
1 2 3
4 5 6
7 8 9
Enter 2nd matrix
2 2 2
3 3 3
4 4 4

Multiplication of A and B matrices is
20 20 20
47 47 47
74 74 74