2013-11-03 68 views
0

好的,我的任務是創建一個程序,從文件中讀取未知矩陣,然後以某種方式計算它的行​​列式。我已經完成了大部分工作,除了從文件中獲取數字之後,這些數字似乎已經變得越來越混亂了。矩陣未被正確讀取

它可能更容易,如果你只是看我的代碼,這是一部分,直到剛剛讀取矩陣後,因爲我說的都是混亂的

#include <stdio.h> 
#include <stdlib.h> 
#include <math.h> 


int main(int argc, char* argv[]) 
{ 
FILE  *input; 
int  i, j, temp; 
int  dim=0; 
double  det; 
const char inp_fn[]="matrix.dat"; 

/*Open File*/ 
input = fopen(inp_fn, "r"); 

/*Find the number of lines and hence dimensions*/ 
while (EOF != (temp = fgetc(input))) 
{ 
    if (temp=='\n') 
    { 
     ++dim; 
    } 
} 

/*Reset pointer to beginning of file and float the matrix*/ 
fseek(input, 0, SEEK_SET); 
float  matrix[dim][dim]; 

/*Check file isn't NULL, if good fill the matrix with the values from the file*/ 
if((input != (FILE*) NULL)) 
{ 
    for(i=0; i<=dim; i++) 
    { 
     for(j=0; j<=dim; j++) 
     { 
      fscanf(input, "%f", &matrix[i][j]); 
     } 
    } 
    fclose(input); 
} 
else 
{ 
    printf("Could not open file!\n"); 
} 

所以揭掉的值,如果你們能看到任何事情請告訴我,我真的很新,所以我可能會錯過一些明顯的東西,謝謝。

回答

2
for(i=0; i<=dim; i++) 
    { 
     for(j=0; j<=dim; j++) 
     { 
      fscanf(input, "%f", &matrix[i][j]); 
     } 
    } 

它必須是i < dimj < dim

數組的索引從0開始,不是1.