1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Calculation of the determinant of a matrix using recursion in C

Discussion in 'C' started by swetab, Jun 22, 2010.

  1. swetab

    swetab New Member

    Joined:
    Jun 22, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    I was unable to run this program due to certain errors...can anyone please rectify the errors...I am unable to return a 2D array properly from a function..help me..:nonod:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    #define max 4
    int a[max][max];
    int s=0;
    int **minor(int arr[][max],int i,int j);
    int det(int arr[][max],int i,int j);
    int main()
    {
        int i,j,n;
       // int **b;
        //b=(int **)malloc((max-1)*sizeof(int));
         int **b,q;
        b=(int **)malloc((max-1)*sizeof(int *));
        for (q= 0; q< max-1; q++){
        b[q]= (int *) malloc((max-1)* sizeof(int));
        }
        printf("enter the elements of the array::\n");
        for(i=0;i<max;i++)
        {
                          for(j=0;j<max;j++)
                          {
                                            scanf("%d",&n);
                                            a[i][j]=n;
                          }
        }
        b=minor(a,2,1);
        printf("the minor array::\n");
        for(i=0;i<max-1;i++)
        {
                            for(j=0;j<max-1;j++)
                            {
                                                printf("data=%d\n",b[i][j]);
                            }
        }
        return(0);
    }
    int **minor(int arr[][max],int i,int j)
    {
        int p[max-1][max-1];
        int **b,q;
        b=(int **)malloc((max-1)*sizeof(int *));
        for (q= 0; q< max-1; q++){
        b[q]= (int *) malloc((max-1)* sizeof(int));
        }
        int k=0,l=0,x=0,y=0;
        printf("katti");
        //b=(int **)malloc(sizeof(a));
        for(x=0;x<max;x++)
        {l=0;
                          for(y=0;y<max;y++)
                          {//printf("data=%d\n",arr[x][y]);
                                            if(x!=i && y!=j)
                                            {
                                                    b[k][l]=arr[x][y];
                                                    printf("data=%d\n",b[k][l]);
                                                    l++;
                                            }
                          }
                          if(x!=i)
                          k++;
        }
        
        return(b);
    }
                                                                                             
    int det(int arr[][max],int i,int j)
    {
        if(sizeof(a)==sizeof(int *))
        {
                                 s=s+arr[0][0];
                                 if(i==max-1)
                                 return(s);
                                 else
                                 return(det(arr,i+1,j));
        }
        else
        
            return(pow(-1,(i+j))*arr[i][j]*det(minor(arr,i,j),i,j));
    
    }
     
    Last edited by a moderator: Jun 22, 2010
  2. swetab

    swetab New Member

    Joined:
    Jun 22, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    I have found an alterantive by passing structures...if u can do it by passing 2d arrays...plz plz plz help...i am posting my program by the use of structures...
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    struct matrix
    {
           int order;
           int **array;
    };
    int creatematrix(struct matrix *p,int ord);
    int calcminor(struct matrix *p,struct matrix *min,int row,int col);
    int calcdet(struct matrix *p);
    void inputmatrix(struct matrix *p);
    int main()
    {
        struct matrix p;
        int ord;
        printf("enter the order::\n");
        scanf("%d",&ord);
        if(creatematrix(&p,ord))
        inputmatrix(&p);
        printf("the determinant calculated=%d\n",calcdet(&p));
        return(0);
    }
    int creatematrix(struct matrix *p,int ord)
    {
        int i;
        p->order=ord;
        p->array=(int **)malloc(ord*sizeof(int *));
        for(i=0;i<ord;i++)
        {
                          p->array[i]=(int *)malloc(ord*sizeof(int));
                          if(!p->array)
                          return(0);
        }
        return(1);
    }
    void inputmatrix(struct matrix *p)
    {
         int i,j;
         for(i=0;i<p->order;i++)
         {
                                for(j=0;j<p->order;j++)
                                {
                                                       printf("enter the element::\n");
                                                       scanf("%d",&(p->array[i][j]));
                                }
         }
    }
    int calcdet(struct matrix *p)
    {
        int i,result=0;
        struct matrix min;
        if(p->order==1)
        return(p->array[0][0]);
        for(i=0;i<p->order;i++)
        {
                               if(calcminor(p,&min,0,i))
                               result=result+(pow(-1,i)*(p->array[0][i])*calcdet(&min));
        }
        return(result);
    }
                           
    int calcminor(struct matrix *p,struct matrix *min,int row,int col)
    {
        int i,j,a,b;
    
    if( p->order <= 1 ) return 0;
    if( row >= p->order || col >= p->order )
    return 0;
    
    if( !creatematrix( min, p->order-1 ))
    return 0;
    
    a = b = 0;
    
    for( i = 0; i <p->order; i++ )
    {
    if( i != row )
    {
    b = 0;
    for( j = 0; j <p->order; j++ )
    {
    if( j != col )
    {
    min->array[a][b] = p->array[i][j];
    b++; // Increase column-count of minor
    }
    }
    a++; // Increase row-count of minor
    }
    }
    }
     
    Last edited by a moderator: Jun 22, 2010
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    What errors?
    Which compiler, OS?
     
  4. swetab

    swetab New Member

    Joined:
    Jun 22, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    The compiler is dev c++....
    errors are:
    cannot convert `int**' to `int (*)[4]' for argument `1' to `int det(int (*)[4], int, int)'
     
  5. arbeels

    arbeels New Member

    Joined:
    Dec 13, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I know this a very old thread, but I bumped into the same problem in my own work, and just found a solution. No one has a solution posted elsewhere on the web, so I'm posting it here for the next person who lands here to find...

    Problem: Some libraries want you to pass a multidimensional array as a parameter. You have just the right array as a private field somewhere in one of your classes. You need to provide an accessor function so that the private field can be passed to the library, but C++ won't let you write a function that returns an array.

    The library function is declared as:

    Code:
    void lib_fun (const float x[][4]);
    
    It will not accept arguments of type float* (compiler error).

    You try writing the following function:

    Code:
    float _matrix[4][4];
    float matrix()[4][4] { return _matrix; }
    
    ...but the compiler complains that matrix() returns an array, which is apparently illegal.

    Solution: Make your accessor function return a pointer to the array.

    Sounds simple, right? In fact, you thought that was what you were doing all along when you wrote the accessor described above. What is _matrix anyway other than a pointer with a little syntactic sugar?

    Well, I won't go into the reasons why C++ doesn't allow functions returning an array, since I don't know them myself. Ask Stroustrup. Probably some ambiguity about whether you want value or reference semantics. The way around the restriction is to add one more level of redirection:

    Code:
    float (*matrix()) [4][4]  {  return &_matrix; }
    
    This works, and

    Code:
    lib_fun(*matrix());
    
    will work properly.

    If you want const correctness, the proper definition would be:

    Code:
    const float (*matrix() const) [4][4]  {  return &_matrix; }
    
    The positioning of the second const is not entirely obvious.

    Have fun,

    Alex
     

Share This Page