Last active
June 3, 2016 11:57
-
-
Save slavaceornea/f8c60d9c2821fd2797e3cc2ed9212838 to your computer and use it in GitHub Desktop.
Java class that calculates the absolute valuefor the difference between the sums of two diagonals of a matrix of size NxN.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.*; | |
import java.text.*; | |
import java.math.*; | |
import java.util.regex.*; | |
//Java class that calculates the absolute value for the difference between the sums of two diagonals of a matrix of size NxN. | |
public class matrixDiagonalDifference { | |
public static void main(String[] args) { | |
Scanner in = new Scanner(System.in); | |
int n = in.nextInt(); | |
int a[][] = new int[n][n]; | |
for(int a_i=0; a_i < n; a_i++){ | |
for(int a_j=0; a_j < n; a_j++){ | |
a[a_i][a_j] = in.nextInt(); | |
} | |
} | |
System.out.println(getAbsoluteValueForDifferenceBetweenDiagonals(a)); | |
} | |
private static int getAbsoluteValueForDifferenceBetweenDiagonals(int[][] matrix) | |
{ | |
int result = 0; | |
for(int i = 0; i < matrix.length; i++) | |
{ | |
result += matrix[i][i]; | |
result -= matrix[matrix.length - i - 1][i]; | |
} | |
return Math.abs(result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment