Hi,can anybody help and guide me on doing this java program?
(Largest rows and columns)Write a program that randomly fills in 0s and 1s into an n-by-n matrix,prints the matrix,and finds the rows and columns with the most 1s.(HINT:Use two ArrayLists to store the row and column indices with the most 1s.
Here are the output required:
Enter the array size n : 4 //input by user
The random array is
0011
0011
1101
1010
The largest row index : 2
The largest column index : 2,3
I've try to write the program but it does not produces the output required.
Any help would be greatly appreciated .
import java.util.Random;
import java.util.Scanner;
public class Main {
// declare a 2 dimensional array or an array of arrays
private static int[][] randArray;
public static void main(String[] args) {
// Create a scanner to get Input from user.
Scanner scanner = new Scanner(System.in);
// gets a number of rows from console input
System.out.print("How many rows? :");
int rows = scanner.nextInt();
// gets # of columns
System.out.print("How many rows? :");
int cols = scanner.nextInt();
randArray = new int[rows][cols];
// loop through the number of rows in thw array
for (int i = 0; i < randArray.length; i++) {
// loop through the elements of the first array in the array
for (int j = 0; j < randArray[0].length; j++) {
// set a random int 0-1 to the array
randArray[i][j] = getRandomInt(0, 1);
// print the number we just assigned
System.out.print(randArray[i][j]);
}
// make a linebreak each row.
System.out.println();
}
}
// quick method I made to get a random int with a min and max
public static int getRandomInt(int min, int max) {
Random rand = new Random();
return rand.nextInt(max-min+1)+min;
}
}