-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq21.cpp
95 lines (80 loc) · 2.18 KB
/
q21.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
/* WAP to multiply two matrices by returning an object from a function. */
#include <iostream>
using namespace std;
class Matrix {
public:
int rows;
int cols;
int **matrix;
void display();
void init_matrix();
Matrix(int r, int c) {
this->rows = r;
this->cols = c;
matrix = new int*[rows];
for (int i = 0; i < rows; i++) {
matrix[i] = new int[cols];
}
}
~Matrix() {
for (int i = 0; i < rows; i++) {
delete [] matrix[i];
}
delete [] matrix;
}
};
void Matrix::init_matrix() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> matrix[i][j];
}
}
}
void Matrix::display() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << matrix[i][j];
cout << "\t";
}
cout << endl;
}
}
Matrix * multiply_matrix(const Matrix& m1, const Matrix& m2) {
Matrix * result = new Matrix(m1.rows, m2.cols);
for (int i = 0; i < m1.rows; i++) {
for (int j = 0; j < m2.cols; j++) {
result->matrix[i][j] = 0;
for (int k = 0; k < m1.cols; k++) {
result->matrix[i][j] += m1.matrix[i][k] * m2.matrix[k][j];
}
}
}
return result;
}
int main() {
int rows1, cols1, rows2, cols2;
cout << "Enter rows in matrix 1: ";
cin >> rows1;
cout << "Enter cols in matrix 1: ";
cin >> cols1;
cout << "Enter rows in matrix 2: ";
cin >> rows2;
cout << "Enter cols in matrix 2: ";
cin >> cols2;
Matrix m1(rows1, cols1), m2(rows2, cols2);
Matrix result(m1.rows, m2.cols);
cout << "Enter values for matrix 1: " << endl;
m1.init_matrix();
cout << "Enter values for matrix 2: " << endl;
m2.init_matrix();
cout << "MATRIX 1: " <<endl;
m1.display();
cout << endl;
cout << "MATRIX 2: " << endl;
m2.display();
cout << endl;
result = *(multiply_matrix(m1, m2));
cout << "Product of matrix 1 and matrix 2: " << endl;
result.display();
return 0;
}