因此,我编写了一些代码来从文件中获取矩阵,将其放入数组并计算其行列式。然而,在运行时,数组似乎没有被填充,程序返回一个 0 矩阵,因此行列式 = 0。我对编程很陌生,所以很难确定确切的原因,但我认为这是与从文件过程中读取的整个过程有关。
该文件只是一个 .data 文件,矩阵元素存储在空格分隔的列中,即:
1.7 1.2 0.5
0.0 -3.2 1.4
3.0 4.0 5.0
这是(我认为是)相关的代码部分,但如果有帮助,我可以发布整个内容。
int main(int argc, char* argv[])
{
FILE *input;
int record, i, j;
int curr_col;
const int dim = DIMENSION;
double entries[dim][dim];
double tmp, determinant;
const char inp_fn[]="matrix.data";
/* Open file */
input = fopen(inp_fn, "r");
/* Check the pointer to file are not NULL */
if(input != (FILE*) NULL)
{
for(j=0; j<dim; j++, fopen(inp_fn, "r"))
{
record = 0; i = 0;
/* Read in records one by one and check the return value of fscanf */
while(fscanf(input,"%lf",&tmp) == 1)
{
curr_col = (record % dim);
if(curr_col==j)
{
/* Copy data points to the array */
entries[i][j] = (double)(tmp);
i++;
}
record++;
}
fclose(input);
}
}
else
printf("*** Could not open input or output file! ***\n");
/* Calculates determinant */
determinant = det(dim, entries);
printf("\nA = \n");
for(i=0; i<dim; i++)
{
for(j=0; j<dim; j++)
{
printf("%.1lf ", entries[i][j]);
}
printf("\n");
}
printf("\n");
printf("det(A) = %.3lf\n", determinant);
}
我得到“无法打开输入或输出文件!” 运行程序时出现错误和空矩阵...帮助!?