Determinant Calculation Program.... HELP PLZ

This is a discussion on Determinant Calculation Program.... HELP PLZ within the C Programming forums, part of the General Programming Boards category; Hi guys, I have to make a program to print out the matrix from the file and its determinant. Here's ...

  1. #1
    ss7
    ss7 is offline
    Registered User ss7's Avatar
    Join Date
    Nov 2006
    Location
    T.O., Canada
    Posts
    11

    Determinant Calculation Program.... HELP PLZ

    Hi guys, I have to make a program to print out the matrix from the file and its determinant. Here's the outline Im supposed to fill out:

    Code:
    #include 
    #define N 10
    #define NMAX 4
    
    double determinant2(double a[N][N]) ;
    double determinant3(double a[N][N]) ;
    double determinant4(double a[N][N]) ;
    void read(double a[N][N], int n) ;
    void print(double a[N][N], int n) ;
    int count_inversions(int b[N], int n)  ;
    
    int 
    main(void)
    {
      double a[N][N] ;
      double determinant ;
      int n ;
    
      /*
        Get n, the size of the square array
       */
    
      /*
        Read the n by n array and then print it.
       */
    
    
      /*
        Find and print the determinant of the matrix
       */
    
    }
    And heres what I got so far:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #define N 10
    #define NMAX 4
    
    
    void read(double a[N][N], int n)
    
    {
    int i,j;
    
    	for (i=0; i<n; i++)
    	{ 
    	for (j=0;j<n;j++)
    	scanf("%lf",&a[i][j]);			
    	}
    }
    
    void print(double a[N][N], int n)
    {
    	int i,j;
    	for (i=0; i<n; i++)
    	{ 
    	for (j=0;j<n;j++)
    	printf("%.2lf\n", a[i][j]);	
    	}
    }
    
    main ()
    {
    int n;
    
    FILE *inp;
    inp = fopen("matrix.txt", "r");
    
    double a[N][N];
    fscanf(inp, "%d", &n);
    
    printf("The size of matrix is %d\n", n);
    
    read(a, n);
    print(a, n);
    }
    and the sample of the input file must be in the following format:

    4
    1 2 5 6
    8 5 6 7
    4 5 9 8
    2 8 4 1

    where the first element (4) indicates the size of square matrix(4x4)

    As you can see Im trying to read the matrix from the input file and print it out on the screen, but the errors Im getting are:

    assi2-.c: In function `main':
    assi2-.c:42: parse error before `double'
    assi2-.c:47: `a' undeclared (first use in this function)

    If anyone can help me out with this, it be greatly appreciated. Thank you for looking.
    Last edited by ss7; 11-28-2006 at 08:23 PM.

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    For starters, read and print take 2 arguments, and you're only sending them one.

    Also, I don't believe the way you declare matrix is legal, but don't quote me on that.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    ss7
    ss7 is offline
    Registered User ss7's Avatar
    Join Date
    Nov 2006
    Location
    T.O., Canada
    Posts
    11
    thanks for the reply, I just updated the reworked program and errors, still getting those few.

  4. #4
    ss7
    ss7 is offline
    Registered User ss7's Avatar
    Join Date
    Nov 2006
    Location
    T.O., Canada
    Posts
    11
    I got it to compile without any errors using this code:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #define N 10
    #define NMAX 4
    
    
    void read(double a[N][N], int n)
    {
            int i,j;
    	for (i=0; i<n; i++)
    	{ 
    		for (j=0;j<n;j++)
    		fscanf(inp, "%lf",&a[i][j]);			
    	}
    }
    
    void print(double a[N][N], int n)
    {
    	int i,j;
    	for (i=0; i<n; i++)
    	{ 
    		for (j=0;j<n;j++)
    		printf("%.2lf\n", a[i][j]);	
    	}
    }
    
    main ()
    {
    int n;
    double a[N][N];
    
    FILE *inp;
    inp = fopen("matrix.txt", "r");
    
    fscanf(inp, "%d", &n);
    
    printf("The size of matrix is %d\n", n);
    
    read(a, n);
    print(a, n);
    but I am getting this error while running it......
    http://img95.imageshack.us/img95/5729/errorzx8.jpg

    Any ideas?
    Last edited by ss7; 11-28-2006 at 11:28 PM.

  5. #5
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Your array shouldn't have the ampersand there in your scanf function.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  6. #6
    ss7
    ss7 is offline
    Registered User ss7's Avatar
    Join Date
    Nov 2006
    Location
    T.O., Canada
    Posts
    11
    hey thanks for pointing that out, I got rid of that, and it still has that error while running

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    1. you need an ampersand in scanf
    2. you said - you wanted to read a numbers from file? you should use fscanf then
    3. In this case - fscanf requires file pointer - you should give 3rd parameter to your read function
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    ss7
    ss7 is offline
    Registered User ss7's Avatar
    Join Date
    Nov 2006
    Location
    T.O., Canada
    Posts
    11
    1. ok put back the ampersand
    2. replaced scanf with fscanf in read function
    3. how would the pointer work exactly? Something like this?

    Code:
    void read(double a[N][N], int n)
    
    {
    	int i,j, x;
    	for (i=0; i<n; i++)
    	{ 
    		for (j=0;j<n;j++)
    		fscanf(inp, "%lf", &a[i*x + j]);			
    	}
    }
    Please correct me if im wrong. Thank you.
    Last edited by ss7; 11-28-2006 at 11:31 PM.

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    void read(double a[N][N], int n,FILE* inp)
    
    {
    	int i,j;
    	for (i=0; i<n; i++)
    	{ 
    		for (j=0;j<n;j++)
    		   fscanf(inp, "%lf", &a[i][j]);			
    	}
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    ss7
    ss7 is offline
    Registered User ss7's Avatar
    Join Date
    Nov 2006
    Location
    T.O., Canada
    Posts
    11
    hey thanks a lot for your help vart!

    I think it kinda worked, it prints out the numbers, but I thought it would print it out in the form of matrix, is anything Im missing?

    here is the result screenshot

    http://img172.imageshack.us/img172/9272/outputho6.jpg

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    you add \n after each number - you should print it only at the end of the line
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    ss7
    ss7 is offline
    Registered User ss7's Avatar
    Join Date
    Nov 2006
    Location
    T.O., Canada
    Posts
    11
    thank you, thank you, thank you, I got it..finally........ (and it was supposed to be easy part of an assignment!).

    Figuring out determinants is for tomorrow.

  13. #13
    ss7
    ss7 is offline
    Registered User ss7's Avatar
    Join Date
    Nov 2006
    Location
    T.O., Canada
    Posts
    11
    my question now, is for example lets say I have input file with 3 matrices of sizes 2x2, 3x3 and 4x4. How can I input all 3 matrices at the same time from 1 file? I tried putting while loop in read function but no success:

    Code:
    void read(double a[N][N], int n, FILE* inp)
    
    {
    	int i,j;
    	while(fscanf(inp,"%lf", &a[n][n])!=EOF)
    	{
    		for (i=0; i<n; i++)
    		{ 
    			for (j=0;j<n;j++)
    			fscanf(inp, "%lf", &a[i][j]);		
    		}
    	}
    }
    Code:
    4			
    4 6 8 3
    2 1 3 7
    4 5 7 9
    1 4 2 5
    
    3
    1 5 6
    8 5 2
    3 0 4
    
    2
    1 5
    2 8
    a sameple of input file:

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,672
    > while(fscanf(inp,"%lf", &a[n][n])!=EOF)
    If you just had
    fscanf(inp,"%d", &n );

    Then your two loops, then you would read in just one matrix.

    Assuming your N constant is big enough to hold any possible matrix you may have.

    Returning the value of 'n' to the calling function would be a good idea, so you know how big it is.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    ss7
    ss7 is offline
    Registered User ss7's Avatar
    Join Date
    Nov 2006
    Location
    T.O., Canada
    Posts
    11
    ok this is what I changed it to:

    Code:
    void read(double a[N][N], int n, FILE* inp)
    
    {
    	int i,j;
    	fscanf(inp,"%d", &n);
    	for (i=0; i<n; i++)
    	{ 
    		for (j=0;j<n;j++)
    		fscanf(inp, "%lf", &a[i][j]);			
    	}
    }
    But it wont read the other matrices below the first one from the input file as above

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with basic calculation program.
    By StateofMind in forum C Programming
    Replies: 18
    Last Post: 03-06-2009, 12:44 AM
  2. Program calculation problems?
    By rebel in forum C++ Programming
    Replies: 7
    Last Post: 11-28-2005, 02:31 PM
  3. Wages Calculation Program
    By DrKillPatient in forum C++ Programming
    Replies: 2
    Last Post: 10-18-2005, 07:06 AM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21