nzu %232 %20-%20Array%20Review%202). pdf Back in the Saddle Again\" Problem: A m
ID: 3878233 • Letter: N
Question
nzu %232 %20-%20Array%20Review%202). pdf Back in the Saddle Again" Problem: A matrix is a rectangular arrangement of mimbers. The matrix below has 3 rows and 2 columns, and it's called a 3-by-2 matrix /5 2 3 1 A saddle point of a matrix is any value of the matrix that is both the largest value in its row and the largest value in its column. There are two saddle points in the matrix above: 5 and 8. If the 4 in the last row of the matrix had been a 5, the matrix would have only one saddle point: 8. If the 4 in the last row had been an 8, the matrix would have no saddle points. Finally, if the 8 in the last row has been a 5, the matrix would have the single saddle point, 5. (This is tricky!) Input: Five matrices. Each matrix starts with the number of rows () and columns (c) of the matrix, followed by the r-by-c columns. The first data line below corresponds to the above. All matrices will be smaller than 6-by-6, and all values will be positive integers. Output: Print the saddle points of each matrix. If the matrix has no saddle points, print a message to that effect. If the matrix has more than one saddle point, they may be listed in any order; all must be listed in order to receive credit. If a saddle point appears more than once (as would happen if the 8 were changed to a 5 in the example above), it must be listed only once Sample In Line #2: 3, 3, 3, 2, 4, 1, 1, 6, 4, 2, 5 Line #5: 3 Sample Output: OutpExplanation / Answer
Ans : )
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
class Test3
{
// Method to find saddle point
static String findSaddlePoint(int mat[][ ], int n,int c)
{
boolean status=false;
String str="";
// Process all rows one by one
for (int i = 0; i < n; i++)
{
// Find the minimum element of row i.
// Also find column index of the minimum element
int min_row = mat[i][0], col_ind = 0;
for (int j = 1; j < c; j++)
{
if (min_row < mat[i][j])
{
min_row = mat[i][j];
col_ind = j;
}
}
// Check if the minimum element of row is also
// the maximum element of column or not
int k;
for (k = 0; k < n; k++)
{
// Note that col_ind is fixed
if (min_row < mat[k][col_ind])
{
break;
}
}
// If saddle point is present in this row then
// print it
if (k == n)
{
str=str+String.valueOf(min_row)+",";
status=true;
}
}
if(!status)
str="None";
return str;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String matStr1="";
boolean mat1Status=true;
System.out.println("Enter Matrix 1 size (rows and columns) should be less than (<=6 and <=6)");
int r1=sc.nextInt();
int c1=sc.nextInt();
int mat1[][]=new int[r1][c1];
if(r1<=6&&c1<=6){
System.out.println("Enter elements("+(r1*c1)+") for Matrix 1 :");
for(int i=0;i<r1;i++){
for(int j=0;j<c1;j++){
int temp=sc.nextInt();
if(temp<0)
{
mat1Status=false;
System.out.println("Should be positive number");
break;
}
mat1[i][j]=temp;
matStr1=matStr1+String.valueOf(mat1[i][j])+" ";
}
matStr1=matStr1+" ";
}
}
String matStr2="";
boolean mat2Status=true;
System.out.println("Enter Matrix 2 size (rows and columns) should be less than (<=6 and <=6)");
int r2=sc.nextInt();
int c2=sc.nextInt();
int mat2[][]=new int[r2][c2];
if(r2<=6&&c2<=6){
System.out.println("Enter elements("+(r2*c2)+") for Matrix 2 :");
for(int i=0;i<r2;i++){
for(int j=0;j<c2;j++){
int temp=sc.nextInt();
if(temp<0)
{
mat2Status=false;
System.out.println("Should be positive number");
break;
}
mat2[i][j]=temp;
matStr2=matStr2+String.valueOf(mat2[i][j])+" ";
}
matStr2=matStr2+" ";
}
}
String matStr3="";
boolean mat3Status=true;
System.out.println("Enter Matrix 3 size (rows and columns) should be less than (<=6 and <=6)");
int r3=sc.nextInt();
int c3=sc.nextInt();
int mat3[][]=new int[r3][c3];
if(r3<=6&&c3<=6){
System.out.println("Enter elements("+(r3*c3)+") for Matrix 3 :");
for(int i=0;i<r3;i++){
for(int j=0;j<c3;j++){
int temp=sc.nextInt();
if(temp<0)
{
mat3Status=false;
System.out.println("Should be positive number");
break;
}
mat3[i][j]=temp;
matStr3=matStr3+String.valueOf(mat3[i][j])+" ";
}
matStr3=matStr3+" ";
}
}
String matStr4="";
boolean mat4Status=true;
System.out.println("Enter Matrix 4 size (rows and columns) should be less than (<=6 and <=6)");
int r4=sc.nextInt();
int c4=sc.nextInt();
int mat4[][]=new int[r4][c4];
if(r4<=6&&c4<=6){
System.out.println("Enter elements("+(r4*c4)+") for Matrix 4 :");
for(int i=0;i<r4;i++){
for(int j=0;j<c4;j++){
int temp=sc.nextInt();
if(temp<0)
{
mat4Status=false;
System.out.println("Should be positive number");
break;
}
mat4[i][j]=temp;
matStr4=matStr4+String.valueOf(mat4[i][j])+" ";
}
matStr4=matStr4+" ";
}
}
String matStr5="";
boolean mat5Status=true;
System.out.println("Enter Matrix 5 size (rows and columns) should be less than (<=6 and <=6)");
int r5=sc.nextInt();
int c5=sc.nextInt();
int mat5[][]=new int[r5][c5];
if(r5<=6&&c5<=6){
System.out.println("Enter elements("+(r5*c5)+") for Matrix 5 :");
for(int i=0;i<r5;i++){
for(int j=0;j<c5;j++){
int temp=sc.nextInt();
if(temp<0)
{
mat5Status=false;
System.out.println("Should be positive number");
break;
}
mat5[i][j]=temp;
matStr5=matStr5+String.valueOf(mat5[i][j])+" ";
}
matStr5=matStr5+" ";
}
}
String output="Matrix 1 : "+matStr1+" "+"Matrix 2 : "+matStr2+" "+"Matrix 3 : "+matStr3+" "+"Matrix 4 : "+matStr4+" "+"Matrix 5 : "+matStr5+" ";
output=output+" "+"Saddle Points Are :";
System.out.println("Saddle Points Are :");
if(r1<=6&&c1<=6&&mat1Status){
System.out.println("Line #1 :"+findSaddlePoint(mat1, r1,c1));
output=output+" "+"Line #1 :"+findSaddlePoint(mat1, r1,c1);
}else{
System.out.println("Line #1 :Size Should be rows/columns less than or equal 6");
output=output+" "+"Line #1 :Size Should be rows/columns less than or equal 6 or Elements sholud be positive";
}
if(r2<=6&&c2<=6&&mat2Status){
System.out.println("Line #2 :"+findSaddlePoint(mat1, r2,c2));
output=output+" "+"Line #2 :"+findSaddlePoint(mat2, r2,c2);
}else{
System.out.println("Line #2 :Size Should be rows/columns less than or equal 6");
output=output+" "+"Line #2 :Size Should be rows/columns less than or equal 6 or Elements sholud be positive";
}
if(r3<=6&&c3<=6&&mat3Status){
System.out.println("Line #3 :"+findSaddlePoint(mat1, r3,c3));
output=output+" "+"Line #3 :"+findSaddlePoint(mat3, r3,c3);
}else{
System.out.println("Line #3 :Size Should be rows/columns less than or equal 6");
output=output+" "+"Line #3 :Size Should be rows/columns less than or equal 6 or Elements sholud be positive";
}
if(r4<=6&&c4<=6&&mat4Status){
System.out.println("Line #4 :"+findSaddlePoint(mat4, r4,c4));
output=output+" "+"Line #4 :"+findSaddlePoint(mat4, r4,c4);
}else{
System.out.println("Line #4 :Size Should be rows/columns less than or equal 6 or Elements sholud be positive");
output=output+" "+"Line #4 :Size Should be rows/columns less than or equal 6 or Elements sholud be positive";
}
if(r5<=6&&c5<=6&&mat5Status){
System.out.println("Line #5 :"+findSaddlePoint(mat5, r5,c5));
output=output+" "+"Line #5 :"+findSaddlePoint(mat5, r5,c5);
}else{
System.out.println("Line #5 :Size Should be rows/columns less than or equal 6 or Elements sholud be positive");
output=output+" "+"Line #5 :Size Should be rows/columns less than or equal 6 or Elements sholud be positive";
}
// create a jframe
JFrame frame = new JFrame("JOptionPane Saddle example");
// show a joptionpane dialog using showMessageDialog
JOptionPane.showMessageDialog(frame,output,"Output",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
Explanaation : Check the above code and give the input in console.....
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.