2016-04-08 43 views
2

我正在研究讀取文件並將數字存儲在數組中的代碼。代碼本身正在工作,但數字沒有正確存儲在數組中,因此,我沒有得到所需的輸出。這是我的代碼:將文本文件讀入C正確順序

/* code to solve a nxn system using the Gauss-Seidel method */ 
#include <stdio.h> 
#include <math.h> 
#include <stdlib.h> 
#include <malloc.h> 

#define MAX_DIM 100 
#define MAX_ITER 500 
#define TOLERANCE 1.e-6 
void gauss_seidel(double **a, double b[], double x[], int n); 
void main() 
{ 
    int i, j, n; 
    int violation_counter, answer; 
    double sum; 
    /* read in data */ 
    n = MAX_DIM + 1; 
    FILE *inf, *onf; 
    char fileName[256]; 
    printf("Enter file Name: "); 
    scanf("%s", fileName); 
    inf = fopen(fileName, "r"); 
    if(inf != NULL){ 
     while (n > MAX_DIM) { 
      fscanf(inf, "%d", &n); 
     } 
     int *violation_rows = (int *)malloc(sizeof(int) * n); 
     double **a = (double **)malloc(sizeof(double *) * n); 
     double *b = (double *)malloc(sizeof(double) * n); 
     double *x = (double *)malloc(sizeof(double) * n); 
     for(i = 0; i < n; ++i){ 
      a[i] = (double *)malloc(sizeof(double) * n); 
     } 
     for (i = 0; i < n; i++) { 
      for (j = 0; j < n; j++) { 
       fscanf(inf, "%lf", &a[i][j], sizeof(a[i][j])); 
       printf("%.2lf ", a[i][j]); 
      } 
      fscanf(inf, "%lf", &b[i], sizeof(b[i])); 
      printf("-> %.2lf\n", b[i]); 
     } 
     printf("\n"); 
     /* test the convergence criterion */ 
     violation_counter = 0; 
     for (i = 0; i < n; i++) { 
      sum = 0.0; 
      for (j = 0; j < n; j++) 
       if (i != j) 
        sum = sum + fabs(a[i][j]); 
      if (fabs(a[i][i]) < sum) { 
       violation_rows[violation_counter] = i; 
       violation_counter = violation_counter + 1; 
      } 
      if (a[i][i] == 0.0) { 
       printf("Found diagonal element equal to zero; rearrange equations; exiting ...\n"); 
       exit(0); 
      } 
     } 

     if (violation_counter > 0) { 
      printf("The Gauss-Seidel convergence criterion is violated in %d rows out of %d\n", violation_counter, n); 
      printf("Specifically, it was violated in rows:\n"); 
      for (i = 0; i < violation_counter; i++) 
       printf("%d ", violation_rows[i]); 

      printf("\n"); 

      printf("Enter 1 if you want to continue; any other number to abort : "); 
      scanf("%d", &answer); 

      if (answer != 1) 
       exit(1); 
      printf("Check results carefully\n\n"); 
     } 
     /* initialize the solution vector -- initial guesses */ 
     for (i = 0; i < n; i++) { 
      fscanf(inf, "%lf", &x[i], sizeof(x[i])); 
      printf("x[%d] = %.2lf\n", i, x[i]); 
     } 

     fclose(inf); 
     /* solve the system */ 
     gauss_seidel(a, b, x, n); 
     /* output solution */ 

     printf("Enter file Name: "); 
     scanf("%s", fileName); 
     onf = fopen(fileName, "w"); 
     for (i = 0; i < n; i++) 
      fprintf(onf, "x[%d]=%f\n", i, x[i]); 
     fprintf(onf, "\n"); 
     fclose(onf); 
    } 
    else{ 
     printf("Can not open %s to read\n", fileName); 
    } 
    return; 
} 

代碼的輸出沒有正確存儲數字。例如,我的文本文件如下:

4 

2 -1 0 0 
-1 3 -2 0 
0 -2 5 -3 
0 0 -3 3 

1 
1.5 
2.5 
1.5 

0 
0 
0 
0 

而且我得到的結果是

2 -1 0 0 -> -1 
3 -2 0 0 -> -2 
5 -3 0 0 -> -3 
3 1 1.5 2.5 -> 1.5 

除了對角線會聚誤差。什麼導致文件不能正確存儲?

編輯:4x4行後面的數字(在第三個塊中的四個:1,1.5,2.5,1.5)應該在箭頭的另一側。

+1

'fscanf'調用中'sizeof'參數應該做些什麼?他們是superflous。 –

+1

我認爲你有一個嵌套錯誤。你應該首先用一個嵌套循環讀取所有'a [j] [i]',然後在一個單獨的循環中讀取所有'b [i]'。你在閱讀'a [j] [i]'的同一個地方閱讀'b [i]',但是輸入文件似乎並不像這樣組織。 (除非錯誤地解釋輸入格式。) –

+0

@Meehm我認爲他們可能會幫助定義每行的大小,以便代碼知道何時中斷。另外,你是否爲'b [i]'提出了一個完全獨立的for循環? –

回答

2

您在數據中讀取的方式與您的輸入文件不匹配。該文件看起來像

n 

a11 a12 a13 
a21 a22 a23 
a31 a32 a33 

b1 
b2 
b3 

,但你讀它,彷彿它是

n 

a11 a12 a13 b1 
a21 a22 a23 b2 
a31 a32 a33 b3 

所以,你應該閱讀在一個單獨的載體b載體。我還建議先閱讀所有內容,並在讀完所有內容後單獨打印:

// Scan first ... 
    for (i = 0; i < n; i++) { 
     for (j = 0; j < n; j++) { 
      fscanf(inf, "%lf", &a[i][j]); 
     } 
    } 

    for (i = 0; i < n; i++) { 
     fscanf(inf, "%lf", &b[i]);   
    } 

    for (i = 0; i < n; i++) { 
     fscanf(inf, "%lf", &x[i]); 
    } 

    // ... then print 
    for (i = 0; i < n; i++) { 
     for (j = 0; j < n; j++) { 
      printf("%.2lf ", a[i][j]); 
     } 
     printf("-> %.2lf\n", b[i]); 
    } 
    printf("\n"); 

    for (i = 0; i < n; i++) { 
     printf("x[%d] = %.2lf\n", i, x[i]); 
    } 
    printf("\n"); 
0

對不起寫這作爲一個答案,而不是評論(沒有信譽還),但我認爲,當你

fscanf(inf, "%lf", &a[i][j], sizeof(a[i][j])); 

可能有一些錯誤的指針二維數組。 (我也想嘗試省略的sizeof()在這裏。)

指點一下這個帖子會談二維數組:

Create a pointer to two-dimensional array

萬事如意!