1404.pti-Adobe Reeder dil Yiew Winduw Help Tools FIll&Sign; Comment original arr
ID: 3853639 • Letter: 1
Question
1404.pti-Adobe Reeder dil Yiew Winduw Help Tools FIll&Sign; Comment original array remains unchanged. To test this method, the main method of your program prompts the user to enter a two- dimensional array and displays the original array followed by the column-sorted array as shown in the sample run. Design the main method of your program such that it allows the user to re- run the program in the same session (run). Document your code, and organize and space the outputs properly. Use escape characters and formatting objects when applicable Export PDF Adobe ExportDf elec POF Fle 8.27 (Column sorting) Implement the following method to sort the columns in a two- dimensional array. A new array is returned and the original array is intact. public static double[] ] sortColumns (doublel] C] m) nths of unlited to Ward omst write a test program that prompts the user to enter a 3 × 3 matrix of double values and displays a new column-sorted matrix. Here is a sample run: Coover create PDF Enter a 3-by-3 matrix row by row: 0.15 0.875 0.375Enter Edit PDF Send Files 0.55 0.005 0.225er 0.30 0.12 0.4Enter The column-sorted array is 0.15 0.0050 0.225 0.3 0.12 0.375 0.55 0.875 0.4 Proaram #5120 ountsh Design and implement a Java program for programming exercise 8.29, pageExplanation / Answer
Sorting2DMartixColumns.java
import java.util.Scanner;
public class Sorting2DMartixColumns {
public static void main(String[] args) {
// Declaring 2-Dimensional Array
double arr[][] = new double[3][3];
// Scanner object is used to get the inputs entered by the user
Scanner sc = new Scanner(System.in);
// Getting the 2-D matrix Entered by the user
System.out.println("Enter a 3x3 matrix row-by-row :");
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
arr[i][j] = sc.nextDouble();
}
}
// Calling the method by passing the user entered matrix as input
arr = sortColumns(arr);
// Displaying the 3x3 matrix after sorting the column values
System.out.println("The Column sorted array :");
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
// Method implementation which sorts the 2-D matrix based on columns
private static double[][] sortColumns(double[][] m) {
double temp;
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m[0].length - 1; j++) {
if (m[j][i] > m[j + 1][i]) {
temp = m[j][i];
m[j][i] = m[j + 1][i];
m[j + 1][i] = temp;
}
}
}
return m;
}
}
_________________________
Output:
Enter a 3x3 matrix row-by-row :
0.15 0.875 0.375
0.55 0.005 0.225
0.30 0.12 0.4
The Column sorted array :
0.15 0.005 0.225
0.3 0.12 0.375
0.55 0.875 0.4
_____________Could you rate me well.Plz .Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.