Thread: store data in binary file then use binary file to find inverse of matrix form of data

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    7

    store data in binary file then use binary file to find inverse of matrix form of data

    anyone help my program isn't working. I need to find inverse of a matrix data in binary file should be used to form matrix.its not finding inverse correctly rest is working good.

    Code:
    #include <stdio.h>
    
    
    void createbin();
    void display();
    void inverse();
    
    
    int main()
    {
        createbin();
        display();
        inverse();
    
    
        return 0;
    }
    
    
    
    
    void createbin()
    {
        int a[3][3],i,j;
        int x;
        FILE *fp = fopen ("file.bin", "wb");
    
    
    
    
        printf("Enter 9 numbers: ");
      for(i=0;i<3;i++)
          for(j=0;j<3;j++)
               scanf("%d",&a[i][j]);
               fwrite (&x, sizeof (x), 1, fp);
        fclose(fp);
    
    
    }
    
    
    void display()
    {
        int a[3][3],i,j;
        float determinant=0;
        int x;
    
    
        printf("\nThe matrix is\n");
      FILE *fp = fopen("file.bin","rb");
      while(1 == fread (&x, sizeof (x), 1, fp))
      {
      for(i=0;i<3;i++)
        {
          printf("\n");
          for(j=0;j<3;j++)
               printf("%d\t",a[i][j]);
            }
      }
    
       fclose(fp);
       
    }
    
    void inverse()
    {
        int a[3][3],i,j,x;
        float determinant=0;
    
    
        FILE *fp = fopen("file.bin","rb");
    
    
           printf("\nInverse of matrix is: \n\n");
    
    
      while(1 == fread (&x, sizeof (x), 1, fp))
            for(i=0;i<3;i++)
          determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3] - a[1][(i+2)%3]*a[2][(i+1)%3]));
    
    
    
    
       for(i=0;i<3;i++)
        {
          for(j=0;j<3;j++)
               printf("%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
           printf("\n");
        }
    
    
    }
    Last edited by Rahul Chawla; 12-07-2013 at 12:18 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > fwrite (&x, sizeof (x), 1, fp);
    But the value you read from the user is stored somewhere else.

    > while(1 == fread (&x, sizeof (x), 1, fp))
    Yet you print something else instead.

    Rather than mashing everything together, write a specific function to write a matrix to a file.
    No printf or scanf calls, just a for loop and fwrite.

    Then do the same for reading a matrix from a file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2013
    Posts
    7
    the thing is if I remove the determinant part from second function. everything works fine. the second function displays the same matrix i input from keyboard. but when I add the determinant part to second part it doesn't find the inverse.

  4. #4
    Registered User
    Join Date
    Dec 2013
    Posts
    7
    I separated the inverse function but doesn't show the correct inverse
    Code:
    void inverse()
    {
        int a[3][3],i,j,x;
        float determinant=0;
    
    
        FILE *fp = fopen("file.bin","rb");
    
    
           printf("\nInverse of matrix is: \n\n");
    
    
      while(1 == fread (&x, sizeof (x), 1, fp))
            for(i=0;i<3;i++)
          determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3] - a[1][(i+2)%3]*a[2][(i+1)%3]));
    
    
    
    
       for(i=0;i<3;i++)
        {
          for(j=0;j<3;j++)
               printf("%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
           printf("\n");
        }
    
    
    }

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So when you were planning to put the data in the matrix? Currently you read into x, and then try to use a bunch of values that you haven't set, then read into x again, then try to use a bunch of values that you haven't set, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 12-06-2013, 11:39 PM
  2. Converting binary data in a txt file into HEX
    By Creative.23 in forum C Programming
    Replies: 6
    Last Post: 04-17-2013, 08:37 PM
  3. store binary data in program...
    By Abda92 in forum C Programming
    Replies: 9
    Last Post: 03-23-2008, 10:33 AM
  4. Binary Data File help
    By khang7 in forum C++ Programming
    Replies: 1
    Last Post: 05-20-2007, 04:33 PM
  5. binary data file
    By tucker81 in forum C Programming
    Replies: 8
    Last Post: 06-08-2006, 12:50 AM