Created
March 10, 2018 14:56
-
-
Save NISH1001/f20352235bd6ea8acadbc2e3b9d8bd7b to your computer and use it in GitHub Desktop.
Find determinant of a NxN matrix (square matrix)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
/* 1 2 3 5 | |
3 4 5 6 | |
6 7 8 7 | |
2 3 4 5 | |
*/ | |
int deterstandard(const int order,int **matrix) | |
{ | |
int i,j,k,val=0; | |
if (order ==2) return val+(deter(matrix)); | |
int (*tmp)[order]=(int (*)[order])matrix; | |
for (k=0;k<order;k++) | |
{ | |
int tmpmatrix[order-1][order-1]; | |
int r=0,c=0; | |
for (i=1;i<order;i++) | |
{ | |
for (j=0;j<order;j++) | |
{ | |
if (j==k) continue; | |
tmpmatrix[r][c++] = tmp[i][j]; | |
} | |
if (c==order-1) | |
{ | |
r++; | |
c=0; | |
} | |
} | |
int sign=((k+1) %2 ==0)?-1:1; | |
val += sign * tmp[0][k] *deterstandard(order-1,tmpmatrix); | |
} | |
return val; | |
} | |
int deter( int (*arr)[2]) | |
{ | |
int i; | |
int val1=1,val2=1; | |
for (i=0;i<2;i++) | |
{ | |
val1 *=arr[i][i]; | |
val2 *=arr[i][2-i-1]; | |
} | |
return val1-val2; | |
} | |
int main() | |
{ | |
int n,i,j; | |
printf("Enter order of square matrix: "); | |
scanf("%d",&n); | |
int mat[n][n]; | |
for(i=0;i<n;i++) | |
{ | |
for(j=0;j<n;j++) | |
{ | |
printf("(%d,%d): ",i+1,j+1); | |
scanf("%d",&mat[i][j]); | |
} | |
} | |
printf("\nDeterminant: %d\n",deterstandard(n,mat)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment