-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsaraus.cpp
72 lines (63 loc) · 1.04 KB
/
saraus.cpp
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
66
67
68
69
70
71
#include<stdio.h>
#include<malloc.h>
int diagonalelem1(int **a,int n,int *i1,int*j1)
{
*i1=(++*i1)%n;
*j1=(++*j1)%n;
return a[*i1][*j1];
}
int diagonalelem2(int **a,int n,int *i1,int*j1)
{
*i1=(++*i1)%n;
*j1=(*j1-1);
if(*j1<0)
*j1=n-1;
return a[*i1][*j1];
}
int sarrus(int **a,int n)
{
int i,j,k,m,t,l,i1,j1,d1=0,d2=0,c;
for(j=0;j<n;j++)
{
i1=0;
j1=j;
l=a[i1][j];
for(m=1;m<n;m++)
{
k=diagonalelem1(a,n,&i1,&j1);
l*=k;
}
d1+=l;
}
j=n-1;
for(c=1;c<=n;c++)
{
i1=0;
j1=j;
l=a[i1][j];
for(m=1;m<n;m++)
{
k=diagonalelem2(a,n,&i1,&j1);
l*=k;
}
d2+=l;
j=(j+1)%n;
}
return d1-d2;
}
int main()
{
int **a,n,i,j;
printf("enter the dimension of square matrix:");
scanf("%d",&n);
a=(int**)malloc(n*sizeof(int*));
for(i=0;i<n;i++)
a[i]=(int*)malloc(n*sizeof(int));
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
printf("enter element in %d row and %d column:",(i+1),(j+1));
scanf("%d",&a[i][j]);
}
printf("the result of determinant is %d",sarrus(a,n));
}