Question
How do I modify this to let the user input the size of the matrix? The lowest size is 4 and the largest size is
How do I modify this to let the user input the size of the matrix? The lowest size is 4 and the largest size is 16.
import java.util.*;
public class Test1
{
public static void main(String[] args)
{
int[][] matrix = new int[4][4]; // create 4 by 4 matrix (need user input???)
// generate 1's and 0's for each each rows and columns
// and track largest row index with the most ones
int largestRI = 0;
int largest = -1;
for (int i = 0; i < matrix.length; i++)
{
int rowCount = 0;
for (int k = 0; k < matrix[i].length; k++)
{
matrix[i][k] = (int)(Math.random() * 2);
rowCount += matrix[i][k];
}
if (rowCount > largest)
{
largestRI = i;
largest = rowCount;
}
}
// find largest column index
int largestCI = 0;
largest = -1;
for (int k = 0; k < matrix[0].length; k++)
{
int columnCount = 0;
for (int i = 0; i < matrix.length; i++)
{
columnCount += matrix[i][k];
}
if (columnCount > largest)
{
largest = columnCount;
largestCI = k;
}
}
// display matrix
for (int i = 0; i < matrix.length; i++)
{
for (int k = 0; k < matrix[i].length; k++)
{
System.out.printf("%d", matrix[i][k]);
}
System.out.printf(" ");
}
System.out.println("The largest row index: " + largestRI);
System.out.println("The largest column index: " + largestCI);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started