-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain2221.c
More file actions
118 lines (105 loc) · 2.15 KB
/
Copy pathmain2221.c
File metadata and controls
118 lines (105 loc) · 2.15 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//寫副程式要用動態陣列的話 主程式 和副程式都要有相應的動態指標陣列 void name (type*,type*)指的是回傳的資料型態
#include <stdio.h>
#include <stdlib.h>
void arraysum(int**,int**);
int main()
{
int i,j,x,z;
int a,b;
int count = 0;
FILE *fpr;
//開檔
fpr=fopen("Hello.txt","r");
//讀檔
fscanf(fpr,"%d",&a); // 行
fscanf(fpr,"%d",&b); // 列
printf("%d\n", a);
printf("%d\n", b);
int **arr1;
arr1 = malloc(b*sizeof(int*)); // G 二維矩陣
for(i=0;i<b;i++)
{
arr1[i]=malloc(a*sizeof(int));
}
int **arr2;
arr2 = malloc(b*sizeof(int*)); // H 二維矩陣
for(i=0;i<(a-b);i++)
{
arr2[i]=malloc(a*sizeof(int));
}
int **ans;
ans = malloc(b*sizeof(int*)); // G 二維矩陣
for(i=0;i<b;i++)
{
ans[i]=malloc(a*sizeof(int));
}
printf("\n");
for (i=0;i<b;i++)
{
for(j=0;j<a;j++)
{
fscanf(fpr,"%d", &arr1[i][j]); // 讀取資料
printf("%d", arr1[i][j]);
}
printf("\n");
}
printf("\n");
for (i=0;i<b;i++)
{
for(j=0;j<a;j++)
{
fscanf(fpr,"%d", &arr2[i][j]); // 讀取資料
printf("%d", arr2[i][j]);
fflush(stdout);
}
printf("\n");
}
printf("\n");
arraysum(&arr1[0],&arr2[0]);
for (i=0;i<b;i++)
{
for(j=0;j<a;j++)
{
printf("%d", ans[i][j]);
}
printf("\n");
}
fclose(fpr);
return 0;
}
void arraysum(int **arr1,int **arr2){
int i,j,x,z;
int a,b;
int count = 0;
FILE *fpr;
//開檔
fpr=fopen("Hello.txt","r");
//讀檔
fscanf(fpr,"%d",&a); // 行
fscanf(fpr,"%d",&b); // 列
// printf("%d\n", a);
// printf("%d\n", b);
/*arr1 = malloc(b*sizeof(int*)); // G 二維矩陣
for(i=0;i<b;i++)
{
arr1[i]=malloc(a*sizeof(int));
}
arr2 = malloc(b*sizeof(int*)); // H 二維矩陣
for(i=0;i<(a-b);i++)
{
arr2[i]=malloc(a*sizeof(int));
}
上面這一段多打得 其實不用打*/
int **ans;
ans = malloc(b*sizeof(int*)); // G 二維矩陣
for(i=0;i<b;i++)
{
ans[i]=malloc(a*sizeof(int));
}
for(int i=0; i<b;i++){
for(int j=0; j<a; j++){
ans[i][j]=arr1[i][j]+arr2[i][j];
}
}
fclose(fpr);
}