Thread: Finding determinant

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    8

    Finding determinant

    Hi,
    I'm trying to write a program that finds the determinant of a matrix A recursively, however, I keep getting error messages.

    My code:
    Code:
    #define n 3
    #include <stdio.h>
    #include <math.h>
    
    int det(int A[][n], int n)  <-- Line 5
    {
        int i, j, k, h, d = 0;
        int m[n-1][n-1] = {0};
    
        if (n == 2)
            d = A[0][0]*A[1][1] - A[1][0]*A[0][1];  <-- Line 11
        else 
        {
            d = 0;
            for (k = 0 ; k < n ; k++)
            {
                /* Create minor matrix */
                for (i = 1 ; i < n ; i++)
                {
                    h = 0;
                    for (j = 0; j < n; j++)
                    {
                        if (j == k)
                        	continue;
                        m[i-1][h] = A[i][j];
                        h++;
                    }
                }
                
                /* sum of (+/-)cofactor * minor matrix */
                d += pow(-1.0, k) * A[0][k] * det(m, n-1);
            }
        }
        return d;
    }
    
    int main()
    {
       int determinant;
       int A[n][n] = {{3,-1,-1},{2,-1,1},{1,-3,2}};
    
       determinant = det(A, n);
       printf("d = %d\n", determinant);
       
       return 0;
    }
    I keep getting the error message

    determinant.c:5: error: syntax error before numeric constant
    determinant.c: In function `det':
    determinant.c:11: error: `A' undeclared (first use in this function)

    I can't seem to find out what is wrong with my det function header.

    Please advise.

    Thank you.

    Regards,
    Rayne

  2. #2
    Registered User
    Join Date
    Jul 2007
    Posts
    9
    well you're defining the formal argument to the det function (i.e n) with the same name as the constant
    you defined.
    i.e #define n

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fast Determinant evaluator
    By jack_carver in forum C Programming
    Replies: 2
    Last Post: 12-28-2009, 08:42 PM
  2. matrix determinant
    By roaan in forum C Programming
    Replies: 1
    Last Post: 06-30-2009, 12:44 PM
  3. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  4. determinant?! HELP!!!
    By ankurtrick in forum C Programming
    Replies: 1
    Last Post: 10-08-2004, 09:12 PM
  5. MFC :: Finding Child Window of a CWnd* Object?
    By SyntaxBubble in forum Windows Programming
    Replies: 2
    Last Post: 09-06-2003, 09:06 AM