Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design and implement a Java program for programming exercise 8.26, page 315 (nam

ID: 3681906 • Letter: D

Question

Design and implement a Java program for programming exercise 8.26, page 315 (name it Rowsorting) as described in the problem statement. Write method sortRows0) as specified (you may change the element type to integer). Notice that this method returns a new array, the original array still unchanged. See Chapter 7 for how to sort one-dimensional array. 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 row-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 with different inputs (i.e., use a loop). Document your code, and organize and space the outputs properly. Use escape characters and formatting objects when applicable.

Explanation / Answer

Hi Please find my code and sample run.

import java.util.Scanner;

public class RowSorting {

   public static double[][] sortRows(double[][] m){
       // if m is null
       if(m == null) return null;
      
       int row = m.length; // number of rows
       int col = m[0].length; // number of columns
      
       // creating a new array of same dimension
       double [][]copy = new double[row][col];
      
       // copying elements from original to copy
       for(int i=0; i<row; i++)
           for(int j=0; j<col; j++)
               copy[i][j] = m[i][j];
      
       // calling selection sort on each row
       for(int i=0; i<row; i++)
           selectionSort(copy[i]);
      
       // returning copy array
       return copy;
   }
  
   // selection sort
   public static void selectionSort(double[] a){

       int spot;
       int minimum;

       for(spot=0; spot<a.length; spot++){

           minimum = spot;
           for(int i=spot+1; i<a.length; i++){
               if(a[i] < a[minimum])
                   minimum = i;
           }

           // swaping
           double temp = a[spot];
           a[spot] = a[minimum];
           a[minimum] = temp;
       }
   }
  
   public static void main(String[] args) {
      
       Scanner sc = new Scanner(System.in);
       System.out.print("Enter row number: ");
       int row = sc.nextInt();
       System.out.print("Enter column number: ");
       int col = sc.nextInt();
      
       // creating array
       double arr[][] = new double[row][col];
      
       // getting inputs for array row by row
       String rowInputs;
       System.out.println("Enter a "+row+"x"+col+" matrix row by row: ");
       sc.nextLine(); // reading ' ' character, skipping ' ' from input buffer
       for(int i=0; i<row; i++){
           rowInputs = sc.nextLine(); // taking a line as string
           String entry[] = rowInputs.split("\s+"); // splitting input string by space
           for(int j=0; j<entry.length; j++){
               // converting each string entry into double and storing in array
               arr[i][j] = Double.parseDouble(entry[j].trim());
           }
          
       }
      
       // displaying original matrix
       for(int i=0; i<row; i++){
           for(int j=0; j<col; j++){
               System.out.print(String.format("%.3f", arr[i][j])+" ");
           }
           System.out.println();
       }
      
       // calling sort method
       double[][] sorted = sortRows(arr);
       System.out.println();
      
       // printing sorted rows matrix
       for(int i=0; i<row; i++){
           for(int j=0; j<col; j++){
               System.out.print(String.format("%.3f", sorted[i][j])+" ");
           }
           System.out.println();
       }
   }
}


/*

Sample run:

Enter row number: 3
Enter column number: 3
Enter a 3x3 matrix row by row:
0.15 0.875 0.373
0.55 0.005 0.225
0.30 0.12 0.4
0.150 0.875 0.373
0.550 0.005 0.225
0.300 0.120 0.400

0.150 0.373 0.875
0.005 0.225 0.550
0.120 0.300 0.400

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote