-
Notifications
You must be signed in to change notification settings - Fork 0
/
laplace.c
65 lines (64 loc) · 1.38 KB
/
laplace.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
double determinant(int m, double a[][m])
{
int i=0, j=0;
if(m == 2)
{
return a[0][0]*a[1][1] - a[0][1]*a[1][0];
}
if(m == 1)
{
return a[0][0];
}
double det = 0;
for(j=0; j<m; j++)
{
double b[m-1][m-1]; int jj=0, ii=0, f=0;
for(jj=0; jj < m; jj++)
{
if(jj == j){f++;continue;}
for(ii=0; ii < m-1; ii++)
b[ii][jj-f] = a[ii+1][jj];
}
int sgn = j%2 == 1? -1:1;
det += sgn * a[0][j] * determinant(m-1, b);
//for(ii=0; ii<m-1; ii++)
//{
// for(jj=0; jj<m-1; jj++)
// printf("%d ", b[ii][jj]);
// printf("\n");
//}
//printf("\n\n\n");
}
return det;
}
int main()
{
int i,j,m;
printf("\n\nEnter order of matrix : ");
scanf("%d",&m);
double a[m][m];
printf("\nEnter the elements of matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
// printf("a[%d][%d] = ",i,j);
scanf("%ld",&a[i][j]);
}
}
//m=4;
//int a[4][4] = {{1,2,3,4}, {1,2,3,4}, {4,5,6,1}, {7,8,9,1}};
printf("\n\n---------- Matrix A is --------------\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<m;j++)
{
printf("\t%d \t",a[i][j]);
}
}
printf("\n \n");
printf("\n Determinant of Matrix A is %lf .",determinant(m,a));
//getch();
}