-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix_alloc_malloc.cpp
More file actions
64 lines (54 loc) · 1.4 KB
/
matrix_alloc_malloc.cpp
File metadata and controls
64 lines (54 loc) · 1.4 KB
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
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
int mat1[3][3], mat2[3][3], mat4[3][3];
cout << "Enter matrix 1: " << endl;
for (int i=0; i<3; i++) {
for (int j=0; j<3; j++) {
cin>>mat1[i][j];
}
}
cout << "Enter matrix 2: " << endl;
for (int i=0; i<3; i++) {
for (int j=0; j<3; j++) {
cin>>mat2[i][j];
}
}
int** mat3 = (int**)malloc(3 * sizeof(int*));
for (int i=0; i<3; i++) {
mat3[i] = (int*)calloc(3, sizeof(int));
}
cout << "\nMatrix multiplication is: " << endl;
for (int i=0; i<3; i++) {
for (int j=0; j<3; j++) {
// mat3[i][j] = 0;
for (int k=0; k<3; k++) {
mat3[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
for (int i=0; i<3; i++) {
for (int j=0; j<3; j++) {
cout<<mat3[i][j]<< " ";
}
cout<<endl;
}
cout << "Matrix addition is: " << endl;
for (int i=0; i<3; i++) {
for (int j=0; j<3; j++) {
mat4[i][j] = mat1[i][j] + mat2[i][j];
}
}
for (int i=0; i<3; i++){
for (int j=0; j<3; j++){
cout<<mat4[i][j]<<" ";
}
cout<<endl;
}
for (int i=0; i<3; i++) {
free(mat3[i]);
}
free(mat3);
return 0;
}