C program to find determinant of Matrix | C code champ

C program to find determinant of Matrix | C code champ

7467
14
SHARE

C program to find determinant of Matrix : The determinant of a matrix is a number that can be calculated from the . It tells us things about the matrix that are useful in systems of linear , in calculus and more. First of all, if you want to calculate the determinant of matrix then your matrix should be an square matrix i.e. MxM , otherwise determinant will not exist.

If you wish to calculate determinant of 3×3 matrix then formula will be something like below :

Say 3×3 matrix is :

C program to find determinant of Matrix | C code champ
Sample 3×3 Matrix

Then its determinant will be like below:

C program to find determinant of Matrix | C code champ
Determinant of 3×3 matrix
Note : |  |  denotes determinant . That means 3 x 3 matrix has been broken into 3 small determinants

Now consider an example of 4×4 matrix :

C program to find determinant of Matrix | C code champ
Determinant of 4×4 Matrix

Notice the + – + – pattern (+a… -b… +c… -d…). This is important to remember.

The pattern continues for 5×5 matrices and higher.

Now let us see how to write a MxM . The program will accept the order of matrix i.e. M as input from user and then ask user to enter the values of matrix elements. Then it will calculate the determinant of the matrix and returns the output.

C program to find determinant of Matrix :

#include<conio.h>
#include<stdio.h>

int a[20][20],m;
int determinant(int f[20][20],int a);
int main()
{
  int i,j;
  printf("----------------------------------------------------------------------\n");
  printf("-------------------made by C code champ ------------------------------\n");
  printf("----------------------------------------------------------------------\n");
  printf("\n\nEnter order of matrix : ");
  scanf("%d",&m);
  printf("\nEnter the elements of matrix\n");
  for(i=1;i<=m;i++)
  {
  for(j=1;j<=m;j++)
  {
  printf("a[%d][%d] = ",i,j);
  scanf("%d",&a[i][j]);
  }
  }
  printf("\n\n---------- Matrix A is --------------\n");    
  for(i=1;i<=m;i++)
     {
          printf("\n");
          for(j=1;j<=m;j++)
          {     
               printf("\t%d \t",a[i][j]);
          }
     }
  printf("\n \n");
  printf("\n Determinant of Matrix A is %d .",determinant(a,m));
  getch();
}

int determinant(int f[20][20],int x)
{
  int pr,c[20],d=0,b[20][20],j,p,q,t;
  if(x==2)
  {
    d=0;
    d=(f[1][1]*f[2][2])-(f[1][2]*f[2][1]);
    return(d);
   }
  else
  {
    for(j=1;j<=x;j++)
    {        
      int r=1,s=1;
      for(p=1;p<=x;p++)
        {
          for(q=1;q<=x;q++)
            {
              if(p!=1&&q!=j)
              {
                b[r][s]=f[p][q];
                s++;
                if(s>x-1)
                 {
                   r++;
                   s=1;
                  }
               }
             }
         }
     for(t=1,pr=1;t<=(1+j);t++)
     pr=(-1)*pr;
     c[j]=pr*determinant(b,x-1);
     }
     for(j=1,d=0;j<=x;j++)
     {
       d=d+(f[1][j]*c[j]);
      }
     return(d);
   }
}

We hope you all have enjoyed the to find determinant of Matrix. If you have any issues with the above code or logic of above code then ask us in form of comment.

14 COMMENTS

  1. slt je veux un programme pour calculer det(A)=0, A est une matrice, a un variable . je veux calculer det(A)=0 pour trouver ce variable (je vais tomber sur une polynome de degré 6, et je dois calculer les racines de cette polynome). merci d’avance

  2. /* Little improvement 2 above function – 10×10 is taken instead of 20×20 */

    int determinant(int f[10][10], int x)
    {
    int b[10][10],c[10], i,j,k,s, sign =1, det = 0;

    if(2==x)
    {
    det = f[0][0]*f[1][1] – f[0][1]*f[1][0];
    }
    else
    {
    for (i=0;i<x;i++,sign*=-1)
    {
    for(j=1;j<x;j++)
    {
    for(s=k=0;k<x;k++)
    {
    if(i != k)
    {
    b[j-1][s] = f[j][k];
    s++;
    }
    }
    }

    det += (sign * f[0][i] * determinant(b,x-1));
    }
    }

    return det;
    }

LEAVE A REPLY