Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke, David J. Auer

7th edition

133544621, 133544626, 0-13-354462-1, 978-0133544626

More Books

Students also viewed these Databases questions

Question

Is this public actively seeking information on this issue?

Answered: 1 week ago

Question

What is Centrifugation?

Answered: 1 week ago

Question

To find integral of ?a 2 - x 2

Answered: 1 week ago

Question

To find integral of e 3x sin4x

Answered: 1 week ago

Question

To find the integral of 3x/(x - 1)(x - 2)(x - 3)

Answered: 1 week ago

Question

What are Fatty acids?

Answered: 1 week ago