-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq09.cpp
82 lines (68 loc) · 1.91 KB
/
q09.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
/*PRogram to add two matrices using functions */
#include <iostream>
using namespace std;
int ** allocate_matrix (int rows, int cols) {
int ** matrix = new int*[rows];
for (int i = 0; i < rows; i++) {
matrix[i] = new int[cols];
}
return matrix;
}
void dealloc_matrix (int **matrix, int rows) {
for (int i = 0; i < rows; i++) {
delete [] matrix[i];
}
delete [] matrix;
}
void display_matrix (int **matrix, int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << matrix[i][j] << " \t";
}
cout << endl;
}
}
int ** add_matrices (int ** m1, int ** m2, int rows, int cols) {
int **result = allocate_matrix(rows, cols);
for (int i = 0; i< rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = m1[i][j] + m2[i][j];
}
}
return result;
}
void fill_values(int **matrix, int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> matrix[i][j];
}
}
}
int main() {
int **m1, **m2, **result;
int rows, cols;
cout << "Enter rows of the matrices: ";
cin >> rows;
cout << "Enter cols of the matrices: ";
cin >> cols;
m1 = allocate_matrix(rows, cols);
m2 = allocate_matrix(rows, cols);
cout << "Fill matrix 1 values: \n";
fill_values(m1, rows, cols);
cout << "Fill matrix 2 values: \n";
fill_values(m2, rows, cols);
result = add_matrices(m1, m2, rows, cols);
cout << "Given matrices: " << endl;
cout << "MATRIX 1" << endl;
display_matrix(m1 ,rows, cols);
cout << endl;
cout << "MATRIX 2" << endl;
display_matrix(m2 ,rows, cols);
cout << endl;
cout << "SUM OF MATRICES: " << endl;
display_matrix(result, rows, cols);
dealloc_matrix(m1, rows);
dealloc_matrix(m2, rows);
dealloc_matrix(result, rows);
return 0;
}