C program to find the determinant of n by n matrix.

“You cannot teach a man anything; you can only help him discover it in himself.” -Galileo
This program calculates the determinant of the matrix by using co-factor method which I read in my high school. It utilizes recursion also.

Here are some snapshot of the program.























Source Code:

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

#define clrscr() system("cls");
#define TRUE 1
#define FALSE 0

double det(double **m, int order);
void minor(double **m, int order, int row, int col, double **f_m);
int main(){
    int i,j,order;
    double **m,deter;
    
    printf("\n Enter the order of the matrix : ");
    scanf(" %d", &order);
    
 //Creating the array of above order.
    m=(double **) malloc(order * sizeof(double));
    for(i=0;i<order;i++){
        m[i]=(double *) malloc(order * sizeof(double));
    }
 
 //Storing each element of the matrix in the array
    for(i=0;i<order;i++){
  for(j=0;j<order;j++){
            printf("\n Element [%d][%d] : ",i+1,j+1);
            scanf(" %lg",&m[i][j]);
        }
    }
    //clrscr();
    deter=det(m,order); //deter holds the determinant of the given matrix i.e. m
 
 //Printing the matrix elements
    for(i=0;i<order;i++){
        for(j=0;j<order;j++){
            printf("\t%lg",m[i][j]);
        }
        printf("\n");
    }
    
 //Freeing up the memory allocated for matrix i.e. m
    for(i=0;i<order;i++){
                             free(m[i]);
    }
    free(m);
 
    printf("\n\n Determinant = %lg",deter);
    getch();
    
}

double det(double **m, int order){
    double **x, d=0;
    int i;
    
    if (order==2){
       d=m[0][0]*m[1][1] - m[0][1]*m[1][0]; //Calculating the determinant of 2 by 2 matrix.
       
    } else {
      
      x=(double **) malloc((order-1) * sizeof(double));
      for(i=0;i<order-1;i++){
        x[i]=(double *) malloc((order-1) * sizeof(double));
      }
      
      for(i=0;i<order;i++){
        minor(m,order,0,i,x); //Calculating the minor of m[0][i] and storing it in x matrix.
        d=d + pow(-1,1+(i+1)) * m[0][i] * det(x,order-1); //Calculating determinant of the matrix m. Here," pow(-1,1+(i+1)) * m[0][i] * det(x,order-1) " is the co-factor of m[0][i].
      }
      
      for(i=0;i<order-1;i++){
        free(x[i]);
      }
      free(x);
    }
    
    return(d);
}

void minor(double **m, int order, int row, int col, double **f_m){
    int i,j,a=0,b=0;
    
 //f_m stores the minor of m
    for(i=0;i<order;i++){
        for(j=0;j<order;j++){
            if(row!=i && col!=j){
                f_m[a][b]=m[i][j];
                    b++;
                    if(b>=order-1){ b=0; a++; }
            }
  }
    }
}


Reference :
[1] http://www.chilimath.com/algebra/advanced/det/more-examples-determinant-3x3.html
[2] http://www.eskimo.com/~scs/cclass/int/sx8.html




Comments

Popular posts from this blog

Solution of Codeforces Round #244 (Div. 2) - Police Recruits in C.

Solution of Codechef - May Challenge 2014 - Chef Jumping in C