-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix-multiplication.cpp
78 lines (77 loc) · 1.46 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//given two matrices of sizes n*m and n1*m1
//find multiplication of the two matrices and print it
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,m,n1,m1;
// cout<<"Enter the dimensions of first matrix\n";
cin>>n>>m;
int a[n][m];
// cout<<"Enter the elements of first matrix\n";
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>a[i][j];
}
}
//cout<<"Enter the dimensions of Second matrix\n";
cin>>n1>>m1;
int b[n1][m1];
//cout<<"Enter the elements of second matrix\n";
for(int i=0;i<n1;i++)
{
for(int j=0;j<m1;j++)
{
cin>>b[i][j];
}
}
if(m!=n1)
{
cout<<"Multiplication of the matrices is not possible";
}
else
{
int c[n][m1];
for(int i=0;i<n;i++)
{
for(int j=0;j<m1;j++)
{
c[i][j]=0;
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m1;j++)
{
for(int k=0;k<n1;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m1;j++)
{
cout<<c[i][j]<<" ";
}
cout<<"\n";
}
}
return 0;
}
//INPUT
//2 3
//1 2 1
//3 4 1
//3 2
//1 2
//1 1
//3 7
//OUTPUT
//6 11
//10 17
//TIME COMPLEXITY OF THE PROGRAM
//O(n^3)