forked from iamshubhamg/Leet-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrayoperation.cpp
88 lines (88 loc) · 1.77 KB
/
arrayoperation.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include<stdio.h>
int main()
{
int arr[10][10],i,j,m,n,k,dsum=0,rsum=0,csum=0,flag=0,transpose[10][10];
long determinant;
scanf("%d %d",&m,&n);
if(m >= 4 && n>=4)
{
printf("Invalid Input\n");
}
else
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&arr[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
dsum += arr[i][j];
}
}
printf("The sum of\n");
printf("Diagonal elements : %d\n", dsum);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
rsum += arr[i][j];
}
printf("Row %d : %d\n",i+1,rsum);
if(rsum != dsum)
{
flag=1;
}
rsum=0;
}
for(j=0;j<n;j++)
{
for(i=0;i<m;i++)
{
csum += arr[i][j];
}
printf("Column %d : %d\n",j+1,csum);
if(csum != dsum)
{
flag=1;
}
csum=0;
}
if(flag == 0)
printf("Magic square\n");
else
printf("Not a magic square\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
transpose[j][i]=arr[i][j];
}
}
printf("Transpose : \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",transpose[i][j]);
}
printf("\n");
}
if(m==2 && n==2)
{
determinant = arr[0][0]*arr[1][1] - arr[1][0]*arr[0][1];
}
else if(m==3 && n==3)
{
determinant = arr[0][0] * ((arr[1][1]*arr[2][2]) - (arr[2][1]*arr[1][2])) -arr[0][1] * (arr[1][0]
* arr[2][2] - arr[2][0] * arr[1][2]) + arr[0][2] * (arr[1][0] * arr[2][1] - arr[2][0] * arr[1][1]);
}
printf("Determinant : %ld", determinant);
}
return 0;
}