-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMajorDiagonalMatrix.java
More file actions
42 lines (31 loc) · 1.06 KB
/
Copy pathMajorDiagonalMatrix.java
File metadata and controls
42 lines (31 loc) · 1.06 KB
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
import java.util.Scanner;
public class MajorDiagonalMatrix {
public static void printMatrix(double[][] matrix) {
for (int y = 0; y < matrix.length; y++) {
for (int x = 0; x < matrix[0].length; x++) {
System.out.print(matrix[x][y] + " ");
}
System.out.println("");
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double[][] matrix = new double[4][4];
for (int y = 0; y < 4; y++) {
String num = scan.nextLine();
String[] numArray = num.split(" ", 0);
for (int x = 0; x < 4; x++) {
matrix[x][y] = Double.parseDouble(numArray[x]);
}
}
printMatrix(matrix);
double average = 0;
for (int y = 0; y < 4; y++) {
average += matrix[y][y];
System.out.println(y);
}
average = average / 4;
System.out.println("The average of the elements in the major diagonal is " + average);
scan.close();
}
}