In this program, we will code for the matrix multiplication. A matrix is a 2-D array which means a list inside a list. To multiply two matrices, their dimension should be the same and it should be a square matrix. A matrix is known as a square matrix when its number of rows is equal to the number of columns.

C++:

#include<iostream.h>
#include< conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
clrscr();
int arr1[3][3],arr2[3][3],arr3[3][3];
cout<<"Enter the Elements of Array 1: "<<endl;
for(int i=0;i<3;i++)
{
    for(int j=0;j<3;j++)
      cin>>arr1[i][j];
}
cout<<"Enter the Elements of Array2:"<<endl;
for(i=0;i<3;i++)
    {for(int j=0;j<3;j++)
        cin>>arr2[i][j];
    }
for(i=0;i<3;i++)
{
    for(int j=0;j<3;j++)
       {arr3[i][j]=0;
       for(int k=0;k<3;k++)
           arr3[i][j]=arr3[i][j]+(arr1[i][k]*arr2[k][j]);
           cout<<arr3[i][j]<<" ";
       }
cout<<endl;
}
getch();
}

Output:

Enter the Elements of Array 1:

2
4
5
6
3
2
4
7
3

Enter the Elements of Array 2:

1
3
4
6
8
6
4
2
6

46 48 62
32 46 54
58 74 76

Python:

arr1=[[0,0,0],[0,0,0],[0,0,0]]
arr2=[[0,0,0],[0,0,0],[0,0,0]]
arr3= [[0,0,0],[0,0,0],[0,0,0]]

print("Enter the Elements of Array 1:")
for i in range(0,3):
    for j in range(0,3):
        element= int(input())
        arr1[i][j]=element

print("Enter the Elements of Array 2:")
for i in range(3):
    for j in range(3):
        element2= int(input())
        arr2[i][j]=element2

for i in range(3):
    for j in range(3):
        for k in range(3):
            arr3[i][j]+=arr1[i][k]*arr2[k][j]
                                               
print(arr3[0],'\n',arr3[1],'\n',arr3[2])

Output:

Enter the Elements of Array 1:

3
4
5
2
54
6
23
45
12

Enter the Elements of Array 2:

23
34
54
5
76
65
34
76
85
[259, 786, 847]
[520, 4628, 4128]
[1162, 5114, 5187]

Leave a Reply

Your email address will not be published. Required fields are marked *