-
Notifications
You must be signed in to change notification settings - Fork 0
/
ds05_a.cpp
43 lines (35 loc) · 1.13 KB
/
ds05_a.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
#include <stdio.h>
#include <malloc.h>
int main() {
FILE* matrix1;
FILE* matrix2;
fopen_s(&matrix1, "m1.txt", "r");
fopen_s(&matrix2, "m2.txt", "r");
int row, column;
fscanf_s(matrix1, "%d", &row);
fscanf_s(matrix2, "%d", &row);
fscanf_s(matrix1, "%d", &column);
fscanf_s(matrix2, "%d", &column);
printf("%d %d\n", row, column);
int** Matrix1; int** Matrix2; int** MatrixOutput;
Matrix1 = (int**)malloc(sizeof(*Matrix1) * row);
Matrix2 = (int**)malloc(sizeof(*Matrix2) * row);
MatrixOutput = (int**)malloc(sizeof(*MatrixOutput) * row);
for (int i = 0; i < row; i++) {
Matrix1[i] = (int*)malloc(sizeof(**Matrix1) * column);
Matrix2[i] = (int*)malloc(sizeof(**Matrix2) * column);
MatrixOutput[i] = (int*)malloc(sizeof(**MatrixOutput) * column);
for (int j = 0; j < column; j++) {
fscanf_s(matrix1, "%d", &Matrix1[i][j]);
fscanf_s(matrix2, "%d", &Matrix2[i][j]);
MatrixOutput[i][j] = Matrix1[i][j] + Matrix2[i][j];
printf("%d ", MatrixOutput[i][j]);
}
printf("\n");
}
//for(int k = 0; k < row; k++){
// free(Matrix1[k]); free(Matrix2[k]); free(MatrixOutput[k]);
//}
fclose(matrix1); fclose(matrix2);
return 0;
}