In: Computer Science
Create an application named MaxRowSum.java that instantiates a 20x20 two-dimentional array of integers, populates it with random integers drawn from the range 1 to 100, and then output the index of the row with the highest sum along all the rows. To support your solution, create a class named RowSumThread.java from which you can instantiate Runnable objects, each of which will sum one row of the two-dimensional array and then place the sum of that row into the appropriate slot of a one-dimensional, 20-element array. To Summarize, your application will:
1. Generate the two-dimensional array of random integers
2. Start 20 concurrent threads, each of which places the sum of one row of the two-dimensional array into the corresponding slot of a one-dimensional array.
3. Onput the index of the row with the maximum value.
import java.util.*;
public class MaxRowSum {
public static void main(String[] args)
{
int total =0;
int temp = 0;
int index =0;
Random rand = new Random();
int[][] arr=new int[20][20];//20 row and 20 column 1. Generate the
two-dimensional array of random integers
int[] sum = new int[20];
for(int i=0;i<20;i++)
{
total = 0;
for(int j=0;j<20;j++)
{
arr[i][j] = rand.nextInt(100) + 1; //generate randon no.between 1
to 100
total = total+arr[i][j];
}
sum[i] = total; //filled the sum matrix that carry the summation of
row................2. Start 20 concurrent threads, each of
which
} //places the sum of one row of the two-dimensional array into the
corresponding slot of a one-dimensional array.
//System.out.println(Arrays.deepToString(arr));
for(int i=0;i<20;i++)
{
if(sum[i] > temp) { //condition to find largest row sum;
temp = sum[i];
index = i;
//System.out.println(index+" "+temp);
}
}
//System.out.println(Arrays.toString(sum));
System.out.println(index); //3. Onput the index of the row with the
maximum value.
}
}