-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryArray.java
More file actions
58 lines (49 loc) · 1.9 KB
/
BinaryArray.java
File metadata and controls
58 lines (49 loc) · 1.9 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
import java.util.Random;
import java.util.Scanner;
public class BinaryArray {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of rows:");
int rows = scanner.nextInt();
System.out.println("Enter the number of columns:");
int cols = scanner.nextInt();
int[][] array = new int[rows][cols];
Random random = new Random();
scanner.close();
// Fill array and count zeros and ones
int[] rowZeros = new int[rows];
int[] rowOnes = new int[rows];
int[] colZeros = new int[cols];
int[] colOnes = new int[cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
array[i][j] = random.nextInt(2);
if (array[i][j] == 0) {
rowZeros[i]++;
colZeros[j]++;
} else {
rowOnes[i]++;
colOnes[j]++;
}
}
}
// Report count of zeros and ones in each row
for (int i = 0; i < rows; i++) {
System.out.println("The number of 1's in row " + (i + 1) + " is " + rowOnes[i]);
System.out.println("The number of 0's in row " + (i + 1) + " is " + rowZeros[i]);
}
// Report count of zeros and ones in each column
for (int j = 0; j < cols; j++) {
System.out.println("The number of 1's in column " + (j + 1) + " is " + colOnes[j]);
System.out.println("The number of 0's in column " + (j + 1) + " is " + colZeros[j]);
}
// Print array
System.out.println("The array is:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
}