-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix_multiplication.cpp
96 lines (78 loc) · 1.74 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
int m;
cout<<"Give the value of N X M : ";
cin>>n>>m;
int a1[n][m];
int a2[m][n];
cout<<"give the value of a1 array >>>.."<<endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cout<<"Give the value of a1["<<i<<"]["<<j<<"] : ";
cin>>a1[i][j];
}
}
//printing the a1 array
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cout<<a1[i][j]<<" ";
}
cout<<endl;
}
//A2 array
cout<<"give the value of a2 array >>>.."<<endl;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cout<<"Give the value of a2["<<i<<"]["<<j<<"] : ";
cin>>a2[i][j];
}
}
//printing the a1 array
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cout<<a2[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
// NOw taking a answer array
int ans[n][n] ;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
ans[i][j] = 0;
}
}
//NOw multiplying them together
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < m; k++)
{
ans[i][j] += a1[i][k] * a2[k][j];
}
}
}
cout<<"answer is >> "<<endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout<<ans[i][j]<<" ";
}
cout<<endl;
}
return 0;
}