Programming Assignment #2 CMPS 280 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. Run and make sure that your program produces exactly the same output for each of the sample inputs listed below.
Sample Input 1:
5
4
6.7 12. 2.0 3.9
14. 4.1 4.9 12.
16. 8.9 10. 2.0
3.2 18. 19. 14.
6.7 7.6 13. 7.4
Sample Output 1:
3.2 4.1 2.0 2.0
Sample Input 2:
3
7
14. 4.5 20. 9.3 17. 4.0 6.3
7.6 20. 7.5 15. 19. 11. 9.9
4.4 2.4 16. 11. 13. 16. 14.
Sample Output 2:
4.4 2.4 7.5 9.3 13.0 4.0 6.3
Sample Input 3:
7
3
20. 8.7 16.
8.2 16. 17.
1.8 6.2 1.2
11. 7.8 13.
14. 8.2 17.
20. 12. 10.
19. 2.5 17.
Sample Output 3:
1.8 2.5 1.2
Sample Input 4:
10
10
6.0 14. 18. 18. 8.0 11. 8.3 5.8 20. 4.2
17. 14. 15. 11. 7.3 5.2 19. 19. 20. 18.
14. 12. 1.8 19. 17. 9.7 19. 11. 4.6 10.
14. 10. 14. 11. 5.3 1.8 3.9 15. 12. 8.8
8.2 3.0 8.7 5.8 12. 2.4 6.5 7.8 15. 16.
5.1 16. 11. 1.4 20. 1.2 9.5 19. 1.3 10.
5.5 17. 5.8 20. 12. 3.0 5.1 1.4 17. 11.
20. 17. 7.4 17. 14. 17. 13. 3.8 4.3 7.5
18. 6.9 4.1 9.4 7.7 13. 20. 4.9 12. 4.8
15. 5.9 8.7 16. 2.7 16. 6.0 4.3 13. 12.
Sample Output 4:
5.1 3.0 1.8 1.4 2.7 1.2 3.9 1.4 1.3 4.2
Sample Input 5:
5
10
8.7 15. 6.1 11. 2.2 9.6 2.2 4.6 7.8 20. 18. 20.
18. 7.0 8.9 14. 5.7 15. 5.0 5.5
8.7 13. 17. 13. 9.7 8.8 3.5 16. 9.2 19.
4.4 8.1 10. 14. 11. 14. 12. 19. 9.0 4.7
9.7 16. 2.5 2.6 12. 18. 9.5 17. 19. 2.6
Sample Output 5:
4.4 8.1 2.5 2.6 2.2 8.8 2.2 4.6 5.0 2.6
Sample Input 6:
10
5
16. 10. 16. 6.9 15.
2.3 2.1 8.8 14. 14.
7.8 9.2 13. 14. 5.7
15. 9.3 7.2 5.0 6.2
17. 5.3 6.4 9.4 4.2
4.7 18. 18. 17. 9.9
12. 5.3 11. 16. 8.2
20. 4.6 17. 16. 7.3
3.3 8.7 5.4 7.5 16.
11. 20. 12. 9.8 20.
Sample Output 6:
2.3 2.1 5.4 5.0 4.2
Note: Could you plz go through this code and let me know if u
need any changes in this.Thank You
_________________
// MinElementInEachColumn.java
import java.util.Scanner;
public class MinElementInEachColumn {
public static void main(String[] args) {
//Declaring variables
int rows,cols;
/*
* Creating an Scanner class object
which is used to get the inputs
* entered by the user
*/
Scanner sc = new
Scanner(System.in);
//Getting the inputs entered by the
user
rows=sc.nextInt();
cols=sc.nextInt();
//Creating a double type 2-D array based on user entered rows and
columns
double matrix[][]=new double[rows][cols];
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
matrix[i][j]=sc.nextDouble();
}
}
for(int i=0;i<cols;i++)
{
System.out.print(columnMin(matrix,i)+" ");
}
}
private static double columnMin(double[][] matrix,
int col) {
double min = matrix[0][col];
//finding minimum on a column
for (int i = 0; i <
matrix.length; i++) {
if (min > matrix[i][col])
min = matrix[i][col];
}
return min;
}
}
__________________________________
Intput:
3
7
14 4.5 20 9.3 17 4.0 6.3
7.6 20 7.5 15 19 11 9.9
4.4 2.4 16 11 13 16 14
4.4 2.4 7.5 9.3 13.0 4.0 6.3
Output:
3
7
14 4.5 20 9.3 17 4.0 6.3
7.6 20 7.5 15 19 11 9.9
4.4 2.4 16 11 13 16 14
4.4 2.4 7.5 9.3 13.0 4.0 6.3
_______________Could you plz rate me well.Thank You
Get Answers For Free
Most questions answered within 1 hours.