質問するログイン新規登録

質問編集履歴

4

プログラミングの全体がこちらです。連立方程式の係数行列を簡約化する関数がlnqgjです。少し変な変数の置き方をしていますが、よろしくお願いします

2018/06/22 12:21

投稿

edatin
edatin

スコア9

title CHANGED
File without changes
body CHANGED
@@ -1,18 +1,106 @@
1
1
  C言語でファイルから行列の値を読み込むときにセグメンテーションエラーが発生しました。
2
2
 
3
3
  ```C言語
4
+ #include <stdio.h>
5
+ #include <math.h>
6
+ #define N 4
7
+ #define M 1
8
+ #define nl 10
9
+
10
+ void lnqgj(double a[][N+M], int n, int m, double epsl, int *isw)
11
+ {
12
+ int j;
13
+ int i;
14
+ int k;
15
+ int ip;
16
+
17
+ double p;
18
+ double w;
19
+ double a_kk;
20
+ double a_ik;
21
+
22
+ for(k = 0 ; k < n ; k++){
23
+ p = 0.0;
24
+
25
+ for(i = k;i < n; i++){
26
+ if(p < fabs(a[i][k])){
27
+ p = fabs(a[i][k]);
28
+ ip = i;
29
+ }
30
+ }
31
+
32
+ if(p <= epsl){
33
+ *isw = 1;
34
+ printf("\n\t isw = %d\n", *isw);
35
+ return ;
36
+ }
37
+
38
+ for(j = k;j < (n + m) ; j++){
39
+ w = a[k][j];
40
+ a[k][j] = a[ip][j];
41
+ a[ip][j] = w;
42
+ }
43
+
44
+ a_kk = a[k][k];
45
+ for(j = k;j < (n + m); j++){
46
+ a[k][j] /= a_kk;
47
+ }
48
+
49
+ for (i = 0; i < n ; i++){
50
+ if(i != k){
51
+ a_ik = a[i][k];
52
+ for(j = k; j<(n + m);j++)
53
+ a[i][j] -= (a_ik * a[k][j]);
54
+ }
55
+ }
56
+ }
57
+
58
+ *isw = 0;
59
+ return ;
60
+ }
61
+
62
+ main(){
4
63
  int a;
5
64
  int b;
6
- int s = 4;
65
+ int s = N;
7
- int t = 1;
66
+ int t = M;
67
+ int *isw;
68
+ double min = 1.0e-5;
8
69
  double z[s][s+t];
70
+ FILE *fpin;
71
+ FILE *fpout;
72
+
73
+ if((fpin = fopen("kadai17.dat", "r"))==0){
74
+ fprintf(stderr, "\n\tSorry! I can't open kadai17.dat\n");
75
+ return 0;
76
+ }
77
+ if((fpout = fopen("kadai17ans.dat", "w"))==0){
78
+ fprintf(stderr, "\n\tSorry! I can't open kadai17ans.dat\n");
79
+ return 0;
80
+ }
81
+
9
82
  for( a = 0; a < 4 ;a++){
10
83
  printf("\n%d\n",a);
11
84
  for( b = 0; b < 5 ;b++){
12
- fscanf(fpin,"%lf",&z[a][b]);
85
+ fscanf(fpin,"%lf",&(z[a][b]));
13
86
  printf("%lf\t",z[a][b]);
14
87
  }
15
88
  }
89
+
90
+ lnqgj(z,s,t,min,isw);
91
+
92
+ fprintf(fpout,"isw = %d\n",*isw);
93
+ for( a = 0; a < 4;a++){
94
+ printf("\n\t%lf\n",z[a][4]);
95
+ fprintf(fpout, "\tX%d =%lf\n",a+1 , z[a][4]);
96
+ }
97
+
98
+ fclose (fpin);
99
+ fclose (fpout);
100
+ return 0;
101
+ }
102
+
103
+
16
104
  ```
17
105
 
18
106
  ### 試したこと

3

読み込んでいるファイルの内容を追加しました

2018/06/22 12:21

投稿

edatin
edatin

スコア9

title CHANGED
File without changes
body CHANGED
@@ -16,6 +16,11 @@
16
16
  ```
17
17
 
18
18
  ### 試したこと
19
+ 読み込んでいるファイルの中身はこちらです
20
+ 3 2 7 1 8
21
+ 1 5 1 -1 5
22
+ 4 1 3 -2 7
23
+ 1 6 4 3 13
19
24
 
20
25
  printfで読み込まれたデータを表示するのですが、a<4とすると4行目の値が表示されず上のprintfで表示している行数のみが表示されます
21
26
 

2

変数aとbの定義を追加しました

2018/06/22 08:31

投稿

edatin
edatin

スコア9

title CHANGED
File without changes
body CHANGED
@@ -1,6 +1,8 @@
1
1
  C言語でファイルから行列の値を読み込むときにセグメンテーションエラーが発生しました。
2
2
 
3
3
  ```C言語
4
+ int a;
5
+ int b;
4
6
  int s = 4;
5
7
  int t = 1;
6
8
  double z[s][s+t];

1

配列zはこの様に定義しています。質問の書き方が間違っていて申し訳ないです

2018/06/22 08:28

投稿

edatin
edatin

スコア9

title CHANGED
File without changes
body CHANGED
@@ -1,7 +1,9 @@
1
1
  C言語でファイルから行列の値を読み込むときにセグメンテーションエラーが発生しました。
2
2
 
3
-
4
- C言語
3
+ ```C言語
4
+ int s = 4;
5
+ int t = 1;
6
+ double z[s][s+t];
5
7
  for( a = 0; a < 4 ;a++){
6
8
  printf("\n%d\n",a);
7
9
  for( b = 0; b < 5 ;b++){
@@ -9,8 +11,8 @@
9
11
  printf("%lf\t",z[a][b]);
10
12
  }
11
13
  }
14
+ ```
12
15
 
13
-
14
16
  ### 試したこと
15
17
 
16
18
  printfで読み込まれたデータを表示するのですが、a<4とすると4行目の値が表示されず上のprintfで表示している行数のみが表示されます