Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Cant figure out why it won't run point out errors in my code please: code: import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class g {

Cant figure out why it won't run

point out errors in my code please:

code:

import java.util.ArrayList;

import java.util.Random;

import java.util.Scanner;

public class g {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Enter the size of the matrix: ");

int[][] binaryMatrix = createMatrix(input.nextInt());

displayMatrix(binaryMatrix);

ArrayList largestRowList = findLargestRow(binaryMatrix);

ArrayList largestColList = findLargestCol(binaryMatrix);

System.out.print("Largest row is " + largestRowList.toString()

+ " Largest colum is " + largestColList.toString());

input.close();

}

public static ArrayList findLargestRow(int[][] binaryMatrix) {

if (binaryMatrix.length == 0) {

System.out.println("Matrix is empty!");

return new ArrayList();

}

ArrayList tempLargestRowList = new ArrayList<>();

int largest = 0;

for (int i = 0; i < binaryMatrix.length; i++) {

int counter = 0;

for (int j = 0; j < binaryMatrix[i].length; i++)

if (binaryMatrix[i][j] == 1)

counter++;

if (counter > largest) {

largest = counter;

tempLargestRowList.clear();

tempLargestRowList.add(i);

}

else if (counter == largest)

tempLargestRowList.add(i);

}

return tempLargestRowList;

}

public static ArrayList findLargestCol(int[][] binaryMatrix) {

if (binaryMatrix.length == 0) {

System.out.println("Matrix is empty!");

return new ArrayList();

}

ArrayList tempLargestRowList = new ArrayList<>();

int largest = 0;

for (int i = 0; i < binaryMatrix.length; i++) {

int counter = 0;

for (int j = 0; j < binaryMatrix[i].length; i++)

if (binaryMatrix[j][i] == 1)

counter++;

if (counter > largest) {

largest = counter;

tempLargestRowList.clear();

tempLargestRowList.add(i);

}

else if (counter == largest)

tempLargestRowList.add(i);

}

return tempLargestRowList;

}

public static void displayMatrix(int[][] binaryMatrix) {

for (int i = 0; i < binaryMatrix.length; i++) {

for (int j = 0; j < binaryMatrix.length; j++)

System.out.print(binaryMatrix[i][j] + " ");

System.out.println();

}

}

public static int[][] createMatrix(int size) {

System.out.println("The random array is");

int[][] tempBinaryMatrix = new int[size][size];

Random randBinary = new Random();

for (int i = 0; i < tempBinaryMatrix.length; i++)

for (int j = 0; j < tempBinaryMatrix[i].length; j++)

tempBinaryMatrix[i][j]= randBinary.nextInt(2);

return tempBinaryMatrix;

}

}

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_step_2

Step: 3

blur-text-image_step3

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

More Books

Students also viewed these Databases questions

Question

What motivated Michael and Sheena to start the EET?

Answered: 1 week ago