-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab33.java
More file actions
116 lines (109 loc) · 3.88 KB
/
Lab33.java
File metadata and controls
116 lines (109 loc) · 3.88 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
import java.util.Scanner;
import java.util.Random;
public class Lab33 {
static Scanner scanner = new Scanner(System.in);
public static void checkSubdiagonal(int[][] matrix,int size){
boolean haveSame = false;
int same = 0;
for(int i=0;i<size-1;i++){
if(matrix[1][0]==matrix[i+1][i]){
same++;
if(same==size-1){
System.out.println("All "+ matrix[1][0] +"s on the subdiagonal");
haveSame = true;
}
}
else break;
}
if(!haveSame) System.out.println("No same numbers on a subdiagonal");
}
public static void checkSuperdiagonal(int[][] matrix,int size){
boolean haveSame = false;
int same = 0;
for(int i=0;i<size-1;i++){
if(matrix[0][1]==matrix[i][i+1]){
same++;
if(same==size-1){
System.out.println("All "+ matrix[0][1] +"s on the superdiagonal");
haveSame = true;
}
}
else break;
}
if(!haveSame) System.out.println("No same numbers on a superdiagonal");
}
public static void checkDiagonal(int[][] matrix,int size){
boolean haveSame = false;
int same = 0;
for(int i=0;i<size;i++){
if(matrix[0][0]==matrix[i][i]){
same++;
if(same==size){
System.out.println("All "+ matrix[0][0] +"s on the diagonal");
haveSame = true;
}
}
else break;
}
if(!haveSame) System.out.println("No same numbers on a diagonal");
}
public static void checkColumn(int[][] matrix,int size){
boolean haveSame = false;
for(int i=0;i<size;i++){
int same = 0;
for(int j=0;j<size;j++){
if(matrix[0][i]==matrix[j][i]){
same++;
if(same==size){
System.out.println("All "+ matrix[0][i] +"s on column " + i);
haveSame = true;
}
}
else break;
}
}
if(!haveSame) System.out.println("No same numbers on a column");
}
public static void checkRow(int[][] matrix,int size){
boolean haveSame = false;
for(int i=0;i<size;i++){
int same = 0;
for(int j=0;j<size;j++){
if(matrix[i][0]==matrix[i][j]){
same++;
if(same==size){
System.out.println("All "+ matrix[i][0] +"s on row " + i);
haveSame = true;
}
}
else break;
}
}
if(!haveSame) System.out.println("No same numbers on a row");
}
public static void checkMatrix(int[][] matrix,int size){
checkRow(matrix, size);
checkColumn(matrix, size);
checkSuperdiagonal(matrix, size);
checkDiagonal(matrix, size);
checkSubdiagonal(matrix, size);
}
public static void main(String[] args){
Random rand = new Random();
boolean inputError = false;
System.out.print("Enter the size for the matrix: ");
int num = scanner.nextInt();
if(num<2) inputError = true;
int matrix[][] = new int[num][num];
for(int i=0;i<num;i++){
for(int j=0;j<num;j++){
matrix[i][j]=rand.nextInt(2);
System.out.print(matrix[i][j]);
if(j!=num-1) System.out.print(" ");
}
System.out.println();
}
if(inputError) System.out.println("INPUT ERROR!");
else checkMatrix(matrix, num);
}
}