-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathExercise08_14.java
93 lines (81 loc) · 2.62 KB
/
Exercise08_14.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
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
package ch_08;
import java.util.*;
/**
* **8.14 (Explore matrix) Write a program that prompts the user to enter the length of a
* square matrix, randomly fills in 0s and 1s into the matrix, prints the matrix, and
* finds the rows, columns, and diagonals with all 0s or 1s. Here is a sample run of
* the program:
* <p>
* Enter the size for the matrix: 4
* 0111
* 0000
* 0100
* 1111
* All 0s on row 1
* All 1s on row 3
* No same numbers on a column
* No same numbers on the major diagonal
* No same numbers on the sub-diagonal
*/
public class Exercise08_14 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the size for the the matrix: ");
int size = in.nextInt();
int[][] matrix = new int[size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
matrix[i][j] = (int) (0.5 + Math.random());
}
}
for (int[] array : matrix) {
System.out.println(Arrays.toString(array));
}
//Check rows
int[] row = checkRows(matrix);
//Check columns
int[] column = checkColumns(matrix);
if (row != null) {
System.out.println("All " + row[0] + " on " + "row " + row[1]);
} else {
System.out.println("No same numbers on a row");
}
if (column != null) {
System.out.println("All " + column[0] + " on " + "column " + column[1]);
} else {
System.out.println("No same numbers on a column");
}
}
static int[] checkColumns(int[][] matrix) {
for (int checkNum = 0; checkNum <= 1; checkNum++) {
for (int i = 0; i < matrix.length; i++) {
if (checkColumn(matrix, i, checkNum)) {
return new int[]{checkNum, i};
}
}
}
return null;
}
static boolean checkColumn(int[][] matrix, int column, int num) {
for (int i = 0; i < matrix.length; i++) {
if (matrix[i][column] != num) {
return false;
}
}
return true;
}
static int[] checkRows(int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
int rowCheck = matrix[i][0];
COLUMN:
for (int j = 0; j < matrix[i].length; j++) {
if (matrix[i][j] != rowCheck) {
break COLUMN;
} else if (j == matrix[i].length - 1) {
return new int[]{rowCheck, i};
}
}
}
return null;
}
}