Program to calculate the determinant of a matrix of any order using a recursive function.

#include "stdio.h"
#include "conio.h"
#include "math.h"

// A square matrix struct
typedef struct
{
int order;
int **array;

} SQRMATRIX;

// Function declarations
int CreateMatrix( SQRMATRIX *p, int order );
void DisplayMatrix( SQRMATRIX *p );
void InputMatrix( SQRMATRIX *p );
int CalcMinor( SQRMATRIX *p, SQRMATRIX *minor, int row, int col );
int CalcDeterminant( SQRMATRIX *p );
void DestroyMatrix( SQRMATRIX *p );

// main( )
int main( )
{
SQRMATRIX p;
int order;

printf("Enter order of matrix: ");
scanf("%d", &order );

if( !CreateMatrix( &p, order ))
{
printf("Matrix couldn't be created.");
return 0;
}

printf("Matrix created.);
InputMatrix( &p );

printf("The matrix is:);
DisplayMatrix( &p );

printf("The determinant of the matrix is: %d", CalcDeterminant( &p
));

getch( );
return 0;
}

// Create matrix of specified order
int CreateMatrix( SQRMATRIX *p, int order )
{
int i;

if( order < 1 ) return 0;
p->order = order;
p->array = (int**)
malloc( order * sizeof( int* )); // Allocate space for each row

if( !p->array )
return 0;

for( i=0; i < order; i++ )
{
p->array[i] = (int*) malloc( order* sizeof( int ));
// Allocate spacefor each column
if( !p->array )
return 0;
}
return 1;
}

// Print matrix in proper format
void DisplayMatrix( SQRMATRIX *p )
{
int i,j;

if( p->order < i =" 0;">order; i++ )
{
for( j = 0; j <>order; j++ )
printf("%5d ", p->array[i][j] );

printf(");
}
}

// Input matrix from user
void InputMatrix( SQRMATRIX *p )
{
int i,j;

for( i = 0; i <>order; i++ )
for( j = 0; j <>order; j++ )
{
printf("Enter element at ( %d, %d ): ", i+1, j+1 );
scanf("%d", &p->array[i][j] );
}
}

/* Calculate the 'minor' of the given matrix at given position.
The minor is the matrix formed by deleting the specified row
and column from the matrix.
*/

int CalcMinor( SQRMATRIX *p, SQRMATRIX *minor, 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( minor, p->order-1 ))
return 0;

a = b = 0;

for( i = 0; i <>order; i++ )
{
if( i != row )
{
b = 0;
for( j = 0; j <>order; j++ )
{
if( j != col )
{
minor->array[a][b] = p->array[i][j];
b++; // Increase column-count of minor
}
}
a++; // Increase row-count of minor
}
}

return 1;
}

/* Calculate the determinant recursively.
The recursive definition is :
det( m ) = Summation( i = 0 to order ) [ (-1)^i * m[0][i] * det(
minor( m[0][i] ))]
*/
int CalcDeterminant( SQRMATRIX *p )
{
int i, result = 0, temp;
SQRMATRIX minor;

if( p->order < 1 )
{
printf("CalcDeterminant( ) : Invalid matrix.");
return 0;
} // The 'stopping' condition
if( p->order == 1 )
return p->array[0][0];

for( i = 0; i <>order; i++ )
{
if( !CalcMinor( p, &minor, 0, i ))
{
printf("CalcDeterminant( ) : Memory allocation failed.");
return 0;
}

result += ( pow( -1, i ) * p->array[0][i] * CalcDeterminant( &minor
));

DestroyMatrix( &minor );
}

return result;
}
// Release allocated memory
void DestroyMatrix( SQRMATRIX *p )
{
int i;

if( p->order < 1 )
return;
for( i = 0; i <>order; i++ )
free( p->array[i] ); // free each columns

free( p->array ); // free each row
p->order = 0;
}

Related Links :

4 comments:

  1. Can any one provide me this program by using simple do-while looop.. this program seem to be very long for me..

    ReplyDelete
  2. can anyone please tell me what is the problem with this program:
    #include
    #include
    #include
    #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));

    }

    ReplyDelete
  3. Awesome code man... u really rocks and thank u sooo much for ur code it helped me a lot.....

    ReplyDelete
  4. #include
    #include
    #include

    #define SIZE 4

    void display(int *a, int size);

    int determinant(int *p, int size);

    int main()
    {

    int a[SIZE][SIZE];
    for(int i=0;i< SIZE;i++)
    for(int j=0;j< SIZE;j++)
    //scanf("%d", a[i][j]);
    a[i][j]=i*j+i+j;

    printf("original 4 square matrix contents \n");
    display((int *)a,SIZE);
    int ret= determinant((int *)a,SIZE);
    printf("\ndeterminant of the matrix=%d\n",ret);
    getchar();


    }

    void display(int *a, int size)
    {
    for(int i=0;i< size;i++)
    {
    for(int j=0;j< size;j++)
    {
    printf("\t %d", *(a+i*size+j));
    }
    printf("\n");
    }
    }


    int determinant(int *p, int size)
    {
    int sign=1;int sum=0;
    if(size==1)
    return *p;
    else
    {
    int * newp=NULL;
    int k=0;
    for(k=0; k<size;k++)
    {
    newp=(int *)malloc(sizeof(int )*(size-1)*(size-1));

    for(int i=1, m=0;i<size;i++,m++ )
    {
    for(int j=0,n=0;j<size;j++)
    {
    if(j==k)
    continue;
    else
    {
    *(newp + m*(size-1) + n)=*(p+i*size+j);
    n++;
    }
    }
    }
    printf("%d th matrix contents \n",k);
    display(newp,size-1);

    sign=pow((double)-1,k);
    printf("\nvalue at address*(p+k)is %d\n",*(p+k));
    int dd=determinant(newp,size-1);
    printf("\nvalue of determinant is %d\n",dd);
    sum=sum+ ((*(p+k)) * sign* dd);
    printf("\nvalue of sum is %d\n",sum);

    }
    return sum;
    }
    }

    ReplyDelete


If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE


More Useful Topics...

 

History Of C..

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.

My 1st Program...


#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t*******Pankaj *******\n");
printf ("\t\t\t********************************\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}

Next Step...


#include
#include

void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");

printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");

getch ();

}

Hits!!!