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;
}
}

Recommended Answers

All 7 Replies

i ran your code , seems to work...

perhaps i didnt get your problem. by the way ,

The largest row index : 2
The largest column index : 2,3

i know i didnt understand that part...

That the problem..I also didn't understand that part
But my lecturer says try to do it 1st...

if you dont understand the problem yourself... it would be better to first clear it out from your teacher.
As it stand now , its hard for us to help out as the problem itself isnt clear. you can post back here after you clear that part out , or mark it solved , 'cause none of us know what to solve in the first place.

i don't understand the problem in ur code.

finds the rows and columns with the most 1s.

It's asking you to
Look at each row of the matrix, count the number of 1's in each row, find the row (or rows) with the most 1's (in this case row 2, with 3 1's compared to 2 1's in the other 3 rows)
then
Look at each column of the matrix, count the number of 1's in each column, find the column (or column) with the most 1's (in this case columns 2 and 3, both have 3 1's)

Tq everyone...i finally figure it out...

Please mark your thread "solved"; don't just leave it pending and apparently still needing help forever.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.