Skip to content

Commit 0814062

Browse files
authored
Updated
1 parent bfaace9 commit 0814062

File tree

1 file changed

+33
-14
lines changed

1 file changed

+33
-14
lines changed

Diagonal-Difference.c

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,30 @@
22
#include <math.h>
33
int main()
44
{
5-
int n,i,j,sum1=0,sum2=0;
5+
int n,i,j,sum1=0,sum2=0; //n denotes the number of rows and columns in the matrix arr.
66

77
scanf("%d", &n);
8-
int a[n][n];
8+
int arr[n][n];
99
for (i=0;i<n;i++)
1010
{
1111
for (j=0;j<n;j++)
1212
{
13-
scanf("%d ", &a[i][j]);
14-
if(a[i][j]>=-100&&a[i][j]<=100)
13+
scanf("%d ", &arr[i][j]);
14+
//Taking diagonal sum of the matrix arr from the both side
15+
if(arr[i][j]>=-100 && arr[i][j]<=100)
1516
{
1617
if(i==j)
1718
{
18-
sum1+=a[i][j];
19+
sum1+=arr[i][j];
1920
}
2021
if(j==(n-1-i))
2122
{
22-
sum2+=a[i][j];
23+
sum2+=arr[i][j];
2324
}
2425
}
2526
}
2627
}
28+
// This code part belongs to the absolute difference between the sums of the matrix's along two diagonals
2729
if((sum1-sum2)<0)
2830
{
2931
printf("%d", (-((sum1)-(sum2))));
@@ -37,12 +39,29 @@ int main()
3739

3840
/*
3941
Sample Input
40-
4
41-
-1 1 -7 -8
42-
-10 -8 -5 -2
43-
0 9 7 -1
44-
4 4 -2 1
45-
46-
Output
47-
1
42+
43+
3
44+
11 2 4
45+
4 5 6
46+
10 8 -12
47+
Sample Output
48+
49+
15
50+
51+
Explanation
52+
53+
The primary diagonal is:
54+
55+
11
56+
5
57+
-12
58+
Sum across the primary diagonal: 11 + 5 - 12 = 4
59+
60+
The secondary diagonal is:
61+
62+
4
63+
5
64+
10
65+
Sum across the secondary diagonal: 4 + 5 + 10 = 19
66+
Difference: |4 - 19| = 15
4867
*/

0 commit comments

Comments
 (0)