Kai
Kai

Reputation: 13

Reading a matrix from a file and calculating the determinant (in C)

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!?

Upvotes: 1

Views: 2866

Answers (2)

nhahtdh
nhahtdh

Reputation: 56809

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:

for(j=0; j<dim; j++, fopen(inp_fn, "r"))

to

for(j=0; j<dim; j++, input = fopen(inp_fn, "r"))

Your current code will open the file and waste the resource, while fclose will encounter error since the file in input has been close in previous iteration.

The code I suggested above will assign the new FILE* from fopen to input.

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 with j):

record = 0;
// Read the file at most (dim * dim) times to fill up the array
// The reading also stops when it cannot read any double number
while(fscanf(input,"%lf",&tmp) == 1 && record < dim * dim)
{
    // Use some math to calculate the cell to put the new entry
    entries[record / dim][record % dim] = tmp; // entries and tmp are double, no need for casting

    record++;
}

// Close the file after done reading
fclose(input);

Note that fopen is only called once before entering the if condition, 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.

Upvotes: 3

William Morris
William Morris

Reputation: 3684

Try using perror("fopen"); to print the error when your opening fails instead of the printf

Also, note that you are reopening the file each time round the for loop.

Change

for(j=0; j<dim; j++, fopen(inp_fn, "r"))

to

for(j=0; j<dim; j++)

Upvotes: 0

Related Questions