Question
Please explain here how this code works and how it does what it was supposed to do (display the rows and columns with the largest
Please explain here how this code works and how it does what it was supposed to do (display the rows and columns with the largest values).
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class Exercise{
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
Random rnd = new Random();
System.out.print("Enter the array size: ");
int n = scnr.nextInt();
int[][]arr = new int[n][n];
int []rowCounts = new int[n];
int []colCounts = new int[n];
ArrayList
ArrayList
for(int i=0; i for(int j=0; j arr[i][j] = rnd.nextInt(2); for(int i=0; i for(int j=0; j System.out.print(arr[i][j]+" "); } System.out.println(); } for(int i=0; i for(int j=0; j if(arr[i][j]==1) rowCounts[i]++; if(arr[j][i]==1) colCounts[i]++; } } int rowLargeCount = rowCounts[0]; int colLargeCount = colCounts[0]; for(int i=0; i if(rowLargeCount rowLargeCount = rowCounts[i]; if(colLargeCount colLargeCount = colCounts[i]; } for(int i=0; i if(rowLargeCount == rowCounts[i]) largestRow.add(i); if(colLargeCount == colCounts[i]) largestCol.add(i); } System.out.println("The largst row is: "+largestRow.toString()); System.out.println("The largst column is: "+largestCol.toString()); } }
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