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.
Hi.. I have written java program foe the above.
MaxRowSum.java
import java.util.Random;
public class MaxRowSum extends Thread{
Random rand = new Random();
int array[][] = new int[20][20];
int sum[] = new int[20];
int max = 0;
int index = 0;
public void run(){
for(int i=0;i<20;i++){
for(int j=0;j<20;j++){
array[i][j] = rand.nextInt(100)+1;
}
}
for(int i=0;i<20;i++){
int s = 0;
for(int j=0;j<20;j++){
s+=array[i][j];
}
sum[i] = s;
if(max
max = s;
index = i;
}
}
System.out.println("Highest sum is:"+max+" of index row"+(index+1));
}
public static void main(String[] args) {
// TODO Auto-generated method stub
MaxRowSum[] n = new MaxRowSum [20];
for (int i = 0; i < 20; i++) {
n[i] = new MaxRowSum();
n[i].start();
}
}
}
Output:
Highest sum is:1241 of index row10
Highest sum is:1124 of index row7
Highest sum is:1268 of index row15
Highest sum is:1291 of index row18
Highest sum is:1258 of index row11