-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
matrix_multiplication.cpp
75 lines (66 loc) · 1.89 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
// C++ program to implement Matrix Multiplication
#include <bits/stdc++.h>
using namespace std;
int main()
{
int r1, c1, r2, c2;
cout << "Enter the number of rows and columns of the first matrix: ";
cin >> r1 >> c1;
cout << "Enter the number of rows and columns of the second matrix: ";
cin >> r2 >> c2;
//If the number of columns of the second matrix and the number of rows of first matrix differ, they cannot be added
if (c1 != r2)
{
cout << "Given Matrices cannot be multiplyable!!!";
return 0;
}
int A[r1][c1], B[r2][c2], C[r1][c2] = {0};
// Input the values of the matrices
cout << "Enter the values of the first matrix\n";
for (int i = 0; i < r1; i++)
{
for (int j = 0; j < c1; j++)
cin >> A[i][j];
}
cout << "Enter the values of the second matrix\n";
for (int i = 0; i < r2; i++)
{
for (int j = 0; j < c2; j++)
cin >> B[i][j];
}
// Multiply both the matrices
for (int i = 0; i < r1; i++)
{
for (int j = 0; j < c2; j++)
{
C[i][j] = 0;
for (int k = 0; k < c1; k++)
C[i][j] += A[i][k] * B[k][j];
}
}
cout << "The resultant matrix is:\n";
for (int i = 0; i < r2; i++)
{
for (int j = 0; j < c2; j++)
cout << C[i][j] << " ";
cout << endl;
}
return 0;
}
/*
Time Complexity: O(r1 * c2 * c1), where 'r1' is the number of rows of first matrix and 'c2' is the number of columns
of second matrix and 'c1' is the number of columns of first matrix
Space Complexity: O(r1 * c2)
SAMPLE INPUT AND OUTPUT
Enter the number of rows and columns of the first matrix: 2 2
Enter the number of rows and columns of the second matrix: 2 2
Enter the values of the first matrix
1 1
1 1
Enter the values of the second matrix
1 1
1 1
The resultant matrix is:
2 2
2 2
*/