forked from kothariji/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix_multiplication.cpp
59 lines (57 loc) · 1.48 KB
/
matrix_multiplication.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//Program to multiply two matrices:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int row1, column1, row2, column2, i, j, k, s=0;
cout<< "Enter the number of rows and columns of the first matrix : ";
cin>> row1>> column1;
int matrix1[row1][column1];
cout<< "Enter the elements of the first matrix row-wise\n";
for(i=0; i<row1; i++)
{
for(j=0; j<column1; j++)
cin>> matrix1[i][j];
}
cout<< "Enter the number of rows and columns of the second matrix : ";
cin>> row2>> column2;
int matrix2[row2][column2];
cout<< "Enter the elements of the second matrix row-wise\n";
for(i=0; i<row2; i++)
{
for(j=0; j<column2; j++)
cin>> matrix2[i][j];
}
cout<< "\nFirst matrix:\n";
for(i=0; i<row1; i++)
{
for(j=0; j<column1; j++)
cout<< matrix1[i][j]<<" ";
cout<< endl;
}
cout<< "\nSecond matrix:\n";
for(i=0; i<row2; i++)
{
for(j=0; j<column2; j++)
cout<< matrix2[i][j]<<" ";
cout<< endl;
}
if(column1!=row2)
cout<<"\nProduct of the given matrices is not possible";
else
{
cout<< "\nProduct of the given matrices\n";
for(i=0; i<row1; i++)
{
for(j=0; j<column2; j++)
{
for(k=0; k<row2; k++)
s+= matrix1[i][k]*matrix2[k][j];
cout<< s<<" ";
s=0;
}
cout<< endl;
}
}
return 0;
}