Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8867115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:57:03+00:00 2026-06-14T16:57:03+00:00

So I’ve written some code to take a matrix from a file, put it

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T16:57:04+00:00Added an answer on June 14, 2026 at 4:57 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am currently running into a problem where an element is coming back from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.