Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: ISC COMPUTER SCIENCE PRACTICAL 2018 QUESTION 2 : (MATRIX ROW SORT)

Tuesday, March 12, 2019

ISC COMPUTER SCIENCE PRACTICAL 2018 QUESTION 2 : (MATRIX ROW SORT)

/*
 Write a program to declare a matrix A[ ] [ ] of order (M xN) where ‘M’ is the number of rows and ‘N’ is the number of columns such that the values of both ‘M’
 and ‘N’ must be greater than 2 and less than 10.
 Allow the user to input integers into this matrix. Perform the following tasks on the matrix:
(a) Display the original matrix.
(b) Sort each row of the matrix in ascending order using any standard sorting technique.
(c) Display the changed matrix after sorting each row. Test your program for the following data and some random data:
 */
JAVA SOURCE CODE


import java.util.*;
public class ISC2018Q2 {
       public static void main(String[] args) {
             int N,M;
             Scanner in = new Scanner(System.in);
             System.out.println("Enter the ORDER of the matrix (MXN):");
             M = in.nextInt();
             N = in.nextInt();
             while( M <=2 || M >= 10 || N <=2 || N >= 10 ) {
                    System.out.println("INVALID INPUT:");
                    System.out.println("Enter the ORDER of the matrix (MXN):");
                    M = in.nextInt();
                    N = in.nextInt();
                    }
             Matrix obj = new Matrix(M,N);
             obj.inputElements();
             System.out.println("ORIGINAL MATRIX:");
             obj.displayMatrix();
             obj.SortMatrix();
             System.out.println("Changed Matrix (after sorting row):");
             obj.displayMatrix();
             in.close();
             }
       }
      
class Matrix{
       Matrix(int M,int N){
             row=M;
             coloumn=N;
             Mat = new int[row][coloumn];
       }
      
       public void SortMatrix() {
             for (int k =0;k<row;k++) {
                    int temp;
                    for (int i =0;i<coloumn;i++) {
                          for(int j=i;j<coloumn;j++) {
                                 if(Mat[k][i]>Mat[k][j]) {
                                       temp = Mat[k][j];
                                       Mat[k][j]=Mat[k][i];
                                       Mat[k][i]=temp;
                                 }
                          }
                    }
                    //Arrays.sort(Mat[i]); you can use these library function
                    // to avoid writing the bubble sort unless the question specify, which the case is in this problem
             }
       }
      
       public void displayMatrix() {
             for(int i = 0; i < row; i++) {
                    for(int j = 0; j < coloumn; j++) {
                          System.out.print(Mat[i][j]+"  ");
                    }
                    System.out.println();
             }
       }
      
       public void inputElements() {
             Scanner in = new Scanner(System.in);
             System.out.println("Enter the elements of the matrix");
             for(int i = 0; i < row; i++) {
                    for(int j = 0; j < coloumn; j++) {
                          Mat[i][j]= in.nextInt();
                    }
             }
            
       }
       private int[][] Mat;
       private int row;
       private int coloumn;
       }           

No comments:

Post a Comment