Program for calculating Determinant of two 2D matrix

#include<stdio.h>                            //INCLUDING HEADER FILES
#include<math.h>

int mat[20][20],index[20][20],n,d[20];                    //DECLARING GLOBAL VARIABLES
void calc(int);                                //FUNCTION calc() DECLERED HERE

main()                                    //FUNCTION main() STARTS HERE
{
int a,i,j;                            //DECLARING LOCAL VARIABLES FOR main()
printf("\n          ENTER THE ORDER ::");            //GETTING THE ORDER OF DETERMINANT
scanf("%d",&n);
printf("\n          ENTER THE MATRIX ::");            //GETTING THE DETERMINANT
for(i=0;i<n;i++)                        //ROWWISE
for(j=0;j<n;j++)
scanf("%d",&mat[i][j]);
for(i=0;i<n;i++)
index[n-1][i]=i;
a=n-1;
calc(a);                            //CALLING THE FUNCTION calc() WITH PARAMETER a

printf("\n          VALUE OF THE DETERMINANT \n\n");        //PRINTING THE DETERMINANT ...
for(i=0;i<n;i++)
{
printf("             ");
for(j=0;j<n;j++)
printf("%5d",mat[i][j]);
printf("\n");
}
printf("\n          IS EQUAL TO %d\n\n",d[a]);            //....AND ITS VALUE
}                                    //END OF main()

void calc(int i)                            //FUNCTION calc()'S DEFINITION STARTS HERE
{
int j,k,t;                            //DECLARING LOCAL VARIABLES FOR calc()
for(j=0;j<=i;j++)
{
t=0;
for(k=0;k<=i;k++)
{
if(j==k)
continue;
else
{
index[i-1][t]=index[i][k];
t++;
}
}
if(i>2)
calc(i-1);                    //FUNCTION calc() RELOADED
if(i==2)
d[i]+=(mat[n-2][index[1][0]]*mat[n-1][index[1][1]]-mat[n-1][index[1][0]]*mat[n-2][index[1][1]])*mat[n-3][index[i][j]]*pow(-1,(n-i-1)+(index[i][j]));
else
d[i]+=mat[n-i-1][index[i][j]]*d[i-1]*pow(-1,(n-i-1)+(index[i][j]));
   }
}                                    //END OF calc()

Advertisements

4 thoughts on “Program for calculating Determinant of two 2D matrix

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s