Code For Alls..!

get the code!!

Saturday, 29 July 2017

2D Matrix Absolute Difference Diagonals Elements Sum

A Square Matrix has N rows and N columns. Get the matrix as input and find the absolute difference between the sum of the values in the two diagonals.
Input Format:
The first line contains N.
Next N lines containing the elements of the matrix with N column values separated by a space.
Output Format:
Absolute difference between the sum of the values in the two diagonals.
Boundary Conditions:
1 <= N <= 100
Example Input/Output 1:
Input:
2
35 80
90 20
Output:
115
Example Input/Output 2:
Input:
4
1 4 4 2
2 2 1 8
8 1 3 5
10 1 8 3 
Output:
5
C code:
#include<stdio.h>
#include <stdlib.h>
int main()
{
int n,i,j,s1=0,s2=0,t;
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
    scanf("%d",&t);
    if(i==j)
    s1+=t;
    if(j==n-1-i)
    s2+=t;
}
printf("%d",abs(s2-s1));
}

No comments:

Post a Comment