So I’ve written some code to take a matrix from a file, put it into an array and calculate its determinant. However, upon running, the array appears to not have been populated and the program returns a 0 matrix and hence the determinant = 0. I’m pretty new to programming so am finding it difficult to pin down exactly why, but I think it’s something to do with the whole reading from a file process.
The file is just a .data file, with the matrix elements stored in space delimited columns, i.e.:
1.7 1.2 0.5
0.0 -3.2 1.4
3.0 4.0 5.0
Here is (what I think is) the relevant section of code, though I can post the whole thing if it helps.
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);
}
I get the “Could not open input or output file!” error and an empty matrix upon running the program… Help!?
Dirty fix
From you code, I see your intent to open the file every time you read a different column. It is inefficient and clunky. You can make it work (only the reading input part, I don’t know about the rest of your code) by changing:
to
Your current code will open the file and waste the resource, while
fclosewill encounter error since the file ininputhas been close in previous iteration.The code I suggested above will assign the new
FILE*fromfopentoinput.Of course, the way above is very inefficient, as I have pointed out at the beginning.
Better way
A better way around, inside the if statement
if(input != (FILE*) NULL)(remove the loop withj):Note that
fopenis only called once before entering theifcondition, and everything is read in one go.You may also want to add a check after reading to make sure that
record == dim * dim– just in case the data provided is not enough.