I tried searching for the solution to this problem for a while now and couldn't find anything. How do I sum the elements above the secondary diagonal in a matrix (just with loops, nothing fancy) in Java?
This is what I tried:
public static void Page106Ex3$H(int[][] mat) {
int sum = 0;
for (int i = 1; i < mat.length; i ) {
for (int j = i-1; j >= 0; j--) {
sum = mat[i][j];
}
}
System.out.println("Sum: " sum);
}
CodePudding user response:
For square matrix of any order you can use this code,
This code has answer for both primary and secondary diagonals and are separated by comments in the code
private static void calcDiagonalSumMatrix(int[][] mat) {
int sum = 0, leftDiagonal = 0, rightDiagonal = mat[0].length - 1;
//This loop is for primary diagonal
for (int[] ints : mat) {
sum = ints[leftDiagonal ];
}
//This loop is for secondary diagonal
for(int [] intArray: mat) {
sum = intArray[rightDiagonal--];
}
//This loop is what you have to use
for(int x = 0; x < matrix.length; x ) {
rightDiagonal--;
for(int y = rightDiagonal; y >=0; y--) {
sum = mat[x][y];
}
}
System.out.println(sum);
}
Explanation :
Three integers variables are declared one to calculate sum then the
leftDiagonal
is to calculate the index of top left to bottom right diagonal and therightDiagonal
is used as index of top right to bottom left diagonal.Then using the enhanced for loops or foreach loops, calculate the sum of the leftDiagonal and that same of right diagonal.
In each for loop for left diagonal the loop first picks the first set of array then the
leftDiagonal
variable which acts as the index will start from 0 and increase by one for each loops and the same goes for therightDiagonal
except the right diagonal starts from theorder - 1
which is the index of the top right element.Then the third loop actually has two loop, the outer loop will be taking the int array elements then the innermost loop will loop through the elements that the first loop took and adds up the elements from each int[] set excluding the secondary diagonal elements.
CodePudding user response:
If the elements on the secondary are not included, the number of columns in the nested loop should be decremented with indexes starting from 0.
public static int sumOver2ndDiagonal(int[][] arr) {
int sum = 0;
for (int i = 0, n = arr.length - 1; i < n; i ) {
for (int j = 0, m = arr[i].length - 1 - i; j < m; j ) {
sum = arr[i][j];
}
}
return sum;
}
Test:
System.out.println(sumOver2ndDiagonal(
new int[][]{
{ 1, 2, 3, 4},
{ 5, 6, 7, 8},
{ 9, 10, 11, 12},
{13, 14, 15, 16}
}
));
// 1 2 3 5 6 9
// -> 26
CodePudding user response:
The secondary diagonal element in row i is present at (mat.length-i-1). In a row all the elements before the element present at secondary diagonal comes above the secondary diagonal we need to add them.
The below solution is considering indexing start from 0.
public static void Page106Ex3$H(int[][] mat) {
int sum = 0;
int n = mat.length;
for (int i = 0; i < n; i ) {
int j = n - i - 2; // index of element just before the
// secondary diagonal element in row i
while(j >= 0){
sum = mat[i][j];
j--;
}
}
System.out.println("Sum: " sum);
}