Untitled
Anonymous
plain_text
07/04/2024 7:35 PM
2.0 KB
28
Indexable
import java.util.Scanner;
public class ArrayCount {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Get the number of rows and columns from the user
System.out.print("Enter the number of rows: ");
int rows = scanner.nextInt();
System.out.print("Enter the number of columns: ");
int columns = scanner.nextInt();
// Create a 2D array
int[][] array = new int[rows][columns];
// Fill the array with random 0s and 1s
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
array[i][j] = (int) (Math.random() * 2);
}
}
// Print the array
System.out.println("The generated array is:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
// Count 0s and 1s in each row and column
for (int i = 0; i < rows; i++) {
int count0Row = 0;
int count1Row = 0;
for (int j = 0; j < columns; j++) {
if (array[i][j] == 0) {
count0Row++;
} else {
count1Row++;
}
}
System.out.println("Row " + (i + 1) + ": " + count0Row + " zeros and " + count1Row + " ones");
}
for (int j = 0; j < columns; j++) {
int count0Col =Editor is loading...
Leave a Comment