-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamic_memory_allocation_for_matrix_function.cpp
75 lines (63 loc) · 1.78 KB
/
dynamic_memory_allocation_for_matrix_function.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
#include <iostream>
using namespace std;
// adding two 2x2 matrices
void addMatrices(int** matrix1, int** matrix2, int** result) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
}
// subtracting two 2x2 matrices
void subtractMatrices(int** matrix1, int** matrix2, int** result) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
result[i][j] = matrix1[i][j] - matrix2[i][j];
}
}
}
// printing a 2x2 matrix
void printMatrix(int** matrix) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
int main() {
// allocating memory for 2x2 matrices
int** matrix1 = new int*[2];
int** matrix2 = new int*[2];
int** result = new int*[2];
for (int i = 0; i < 2; i++) {
matrix1[i] = new int[2];
matrix2[i] = new int[2];
result[i] = new int[2];
}
// Initialize matrices
matrix1[0][0] = 1; matrix1[0][1] = 2;
matrix1[1][0] = 3; matrix1[1][1] = 4;
matrix2[0][0] = 5; matrix2[0][1] = 6;
matrix2[1][0] = 7; matrix2[1][1] = 8;
cout << "Matrix 1:" << endl;
printMatrix(matrix1);
cout << "Matrix 2:" << endl;
printMatrix(matrix2);
cout << "Matrix 1 + Matrix 2:" << endl;
addMatrices(matrix1, matrix2, result);
printMatrix(result);
cout << "Matrix 1 - Matrix 2:" << endl;
subtractMatrices(matrix1, matrix2, result);
printMatrix(result);
// deallocating memory
for (int i = 0; i < 2; i++) {
delete[] matrix1[i];
delete[] matrix2[i];
delete[] result[i];
}
delete[] matrix1;
delete[] matrix2;
delete[] result;
return 0;
}