-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathExercise08_02.java
45 lines (32 loc) · 1.19 KB
/
Exercise08_02.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package ch_08;
import java.util.Scanner;
/**
* 8.2 (Sum the major diagonal in a matrix) Write a method that
* sums all the numbers in the major diagonal in an n � n matrix
* of double values using the following header:
* Write a test program that reads a 4-by-4 matrix and
* displays the sum of all its elements on the major diagonal.
*/
public class Exercise08_02 {
public static double sumMajorDiagonal(double[][] m) {
double sum = 0;
int row = 0;
for (int column = 0; column < m.length; column++) {
sum += m[row++][column];
}
return sum;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double[][] fourByfour = new double[4][4];
System.out.println("Enter a 4 by 4 matrix row by row: ");
for (int row = 0; row < fourByfour.length; row++) {
for (int column = 0; column < fourByfour[row].length; column++) {
fourByfour[row][column] = input.nextDouble();
}
}
input.close();
System.out.println("The sum of the elements in the major diagonal is"
+ ": " + sumMajorDiagonal(fourByfour));
}
}