Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 largestRow = new ArrayList<>();

ArrayList largestCol = new 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

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

Microsoft SQL Server 2012 Unleashed

Authors: Ray Rankins, Paul Bertucci

1st Edition

0133408507, 9780133408508

More Books

Students also viewed these Databases questions

Question

analyze how research and writing unites with design.

Answered: 1 week ago