In: Computer Science
Problem Description:
Write a method that returns the smallest element in a specified column in a matrix using the following header: public static double columnMin(double[][] m, int columnIndex)
Write a program that reads from the keyboard the number of rows and columns, and an array of double floating point numbers with the specified number of rows and columns.
The program will then invoke the columnMin method and displays the minimum value of all columns.
import java.util.Scanner; //reads data from input stream
public class MatrixColumnMin
{
public static double columnMin(double[][] m,int columnIndex)
{
double min=m[0][columnIndex];
int i;
for(i=0;i<m.length;i++)
{
if(min>m[i][columnIndex])
min=m[i][columnIndex];
}
return min;
}
public static void main(String[] args)
{
int rows,columns;
Scanner input=new Scanner(System.in);
System.out.println("Enter rows and columns of matrix");
rows=input.nextInt();
columns=input.nextInt();
double[][] matrix=new double[rows][columns];
System.out.println("Enter elements");
for(int i=0;i<rows;i++)
{
for(int j=0;j<columns;j++)
matrix[i][j]=input.nextDouble();
}
for(int j=0;j<columns;j++)
System.out.println("The minimum element in column "+j+" is
"+columnMin(matrix,j));
}
}
Output