Write a program in java to find the maximum values in each row and column of a two-dimensional array of integers read from the user. First ask the user for the number of rows and columns, then allocate the two-dimensional array of that size. Next read from the user each row of values, store them then output the maximum values for each row (horizontal) and column (vertical). can some one please help me in this program. in java Sample 1 (user inputs are shown in bold italics) How many rows? 5 How many columns? 4 Enter the values of row 0 separated by spaces 52 68 62 75 Enter the values of row 1 separated by spaces 88 52 -84 -68 Enter the values of row 2 separated by spaces -24 51 -67 82 Enter the values of row 3 separated by spaces 14 78 61 97 Enter the values of row 4 separated by spaces 67 -11 4 47 Results: 52 68 62 75 [75] 88 52 -84 -68 [88] -24 51 -67 82 [82] 14 78 61 97 [97] 67 -11 4 47 [67] [88] [78] [62] [97]

Respuesta :

Answer:

//here is code in Java.

import java.util.*;

class Main

{

public static void main (String[] args) throws java.lang.Exception

{

try{

// variable to store row and column size

int row_size,col_size;

Scanner inp=new Scanner(System.in);

System.out.print("How many rows?");

// read the row size

row_size=inp.nextInt();

System.out.print("How many columns?");

//read the column size

col_size=inp.nextInt();

// create an array of size rowxcolumn

int[][] mat=new int[row_size][col_size];

// array to store row max and column max

int max_of_row[]=new int[row_size];

int max_of_col[]=new int[col_size];

// read the elements of array

for(int x=0;x<row_size;x++)

{

int m=Integer.MIN_VALUE;

System.out.println("enter elements of row "+x+" separated by spaces:");

for(int y=0;y<col_size;y++)

{

// find the maximum of each row

mat[x][y]=inp.nextInt();

if(mat[x][y]>m)

m=mat[x][y];

}

max_of_row[x]=m;

}

// print the output in required format

System.out.println("Results:");

for(int a=0;a<row_size;a++)

{

for(int b=0;b<col_size;b++)

{

System.out.print(mat[a][b]+" ");

}

System.out.print("["+max_of_row[a]+"]");

System.out.println();

}

// find the maximum of each column

for(int x=0;x<col_size;x++)

{

int max_c =Integer.MIN_VALUE;

for(int y=0;y<row_size;y++)

{

if(mat[y][x]>max_c)

{

max_c=mat[y][x];

}

max_of_col[x]=max_c;

}

}

// print the max of each column

System.out.println("max of each col:");

for(int a=0;a<col_size;a++)

{

System.out.print("["+max_of_col[a]+"]"+" ");

}

}catch(Exception ex){

return;}

}

}

Explanation:

Read the number of row and column. Create an array of size rowxcolumn. Read the elements of the array and find the max of each row. Save the max of each row in an array. Find the max of each column and save it in another array.Print the required output.

Output:

How many rows?5                                                                                                                

How many columns?4                                                                                                              

enter elements of row 0 separated by spaces:                                                                                

52 68 62 75                                                                                                                    

enter elements of row 1 separated by spaces:                                                                                

88 52 -84 -68                                                                                                                  

enter elements of row 2 separated by spaces:                                                                                

-24 51 -67 82                                                                                                                  

enter elements of row 3 separated by spaces:                                                                                

14 78 61 97                                                                                                                    

enter elements of row 4 separated by spaces:                                                                                

67 -11 4 47                                                                                                                    

Results:                                                                                                                        

52 68 62 75 [75]                                                                                                                

88 52 -84 -68 [88]                                                                                                              

-24 51 -67 82 [82]                                                                                                              

14 78 61 97 [97]                                                                                                                

67 -11 4 47 [67]                                                                                                                

max of each col:                                                                                                                

[88] [78] [62] [97]