Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java method that sums all the numbers in the major diagnol in a matrix o

ID: 3827992 • Letter: W

Question

Write a Java method that sums all the numbers in the major diagnol in a matrix of double values. Consider that m is a square matrix and not null write your code with the following header:
Public static double sumMajorDiagnol(double [ ] [ ] m) Write a Java method that sums all the numbers in the major diagnol in a matrix of double values. Consider that m is a square matrix and not null write your code with the following header:
Public static double sumMajorDiagnol(double [ ] [ ] m)
Public static double sumMajorDiagnol(double [ ] [ ] m)

Explanation / Answer

/*
* method that sums all the numbers in the major diagnol
* in a matrix of double values
*/

public static double sumMajorDiagnol(double [ ] [ ] m) {
   int sz = m.length; //number of rows
   sz = m[0].length<sz?m[0].length:sz; //Matrix must have 1 row, and all rows of the matrix have same same number of column
   double sum = 0;
   for (int i =0; i<sz; i++)
       sum += m[i][i];
   return sum;
}

Hello champ. This code should work under all normal conditions. I have commented the code to make your life easy. If incase you are facing any trouble, please comment below. I shall be glad to help you.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote