-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise08_14.java
More file actions
141 lines (122 loc) · 4.01 KB
/
Copy pathExercise08_14.java
File metadata and controls
141 lines (122 loc) · 4.01 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import java.util.Scanner;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author jorda
*/
public class Exercise08_14 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the length of a square matrix: ");
int length = input.nextInt();
int[][] matrix = new int[length][length];
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix[row].length; col++) {
matrix[row][col] = (int) (Math.random() * 2);
}
}
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix[row].length; col++) {
System.out.print(matrix[row][col]);
}
System.out.println("");
}
int n = CheckCol(matrix);
if (n > -1) {
System.out.println("All " + matrix[0][n] + "'s on column " + n);
} else {
System.out.println("No same numbers on a column");
}
n = checkRow(matrix);
if (n > -1) {
System.out.println("All " + matrix[n][0] + "'s on column " + n);
} else {
System.out.println("No same numbers on a column");
}
n = majorDiagonal(matrix);
if (n > -1) {
System.out.println("All " + n + "'s on a major diagonal");
} else {
System.out.println("No same numbers on a major diagonal");
}
n = SubDiagonal(matrix);
if(n > -1){
System.out.println("All " + n + "'s on a sub-diagonal");
} else {
System.out.println("No same numbers on the sub-diagonal");
}
}
//returns the column index of a column with all the same number. -1 if no match
public static int CheckCol(int[][] matrix) {
int count = 0;
for (int col = 0; col < matrix[0].length; col++) {
count = 0;
for (int row = 0; row < matrix.length; row++) {
count += matrix[row][col];
}
if (count == matrix[0].length || count == 0) {
return col;
}
}
return -1;
}
//returns the row index of the first row that has all the same number
public static int checkRow(int[][] matrix) {
int count = 0;
for (int row = 0; row < matrix.length; row++) {
count = 0;
for (int col = 0; col < matrix[row].length; col++) {
count += matrix[row][col];
}
if (count == matrix.length || count == 0) {
return row;
}
}
return -1;
}
public static int majorDiagonal(int[][] matrix) {
boolean majorDiagonal = true;
int upperLeft = matrix[0][0];
for (int i = 0; i < matrix.length; i++) {
if (matrix[i][i] != upperLeft) {
majorDiagonal = false;
}
}
if (majorDiagonal) {
return upperLeft;
}
int upperRight = matrix[0][matrix.length - 1];
boolean majorDiagonal2 = true;
int colIndex = matrix.length - 1;
for (int i = 0; i < matrix.length; i++) {
if (matrix[i][colIndex] != upperRight) {
majorDiagonal2 = false;
}
colIndex--;
}
if (majorDiagonal2) {
return upperRight;
} else {
return -1;
}
}
public static int SubDiagonal(int[][] m) {
int[] count = new int[2];
count[0] = 0;
count[1] = 0;
for (int i = 0, j = m.length - 1; i < m.length; i++, j--) {
count[m[i][j]]++;
if (count[0] == m.length) {
return 0;
}
if (count[1] == m.length) {
return 1;
}
}
return -1;
}
}