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 PRACTICALS 2008 ~ Matrix Sort and Largest two elements

Sunday, August 3, 2014

ISC COMPUTER SCIENCE PRACTICALS 2008 ~ Matrix Sort and Largest two elements



ISC COMPUTER SCIENCE PRACTICALS 2008 ~ Matrix Sort and Largest two elements

Given a square matrix list [ ] [ ] of order ‘ n ’. The maximum value possible for ‘ n ’
is 20. Input the value for ‘ n ’ and the positive integers in the matrix and perform the
following task:

1. Display the original matrix
2. Print the row and column position of the largest element of the matrix.
3. Print the row and column position of the second largest element of the
matrix.
4. Sort the elements of the rows in the ascending order and display the new
matrix.

Sample data:
INPUT
N = 3
List [] [ ]
5    1    3
7    4    6
9    8    2

OUTPUT

5    1    3
7    4    6
9    8    2

The largest element 9 is in row 3 and column 1
The second largest element 8 is in row 3 and column 2
Sorted list
1    3    5
4    6    7
2    8    9  

import java.util.*;
public class MatrixfirstSecond {
    public static void main(String[] args) {
        int M;
        Scanner in = new Scanner(System.in);
        System.out.println("INPUT THE ORDER OF THE MATRIX:");
        M = in.nextInt();
        while( M > 20 || M < 1 ) {
            System.out.println("OUT OF RANGE, INPUT AGAIN:");
            M = in.nextInt();
            }
        Sorting cm = new Sorting(M);
        cm.inputElements();
        System.out.println();
        cm.displayMatrix();
        System.out.println();   
        cm.maxMin();
        System.out.println();
        cm.rowSort();
        }
    }
class Sorting {
    Sorting(int M) {
        row = M;
        column = M;
        Mat = new int[row][column];
        }
    public void inputElements() {
        System.out.println("Enter the elements of the matrix:");
        Scanner in = new Scanner(System.in);
        for(int i = 0; i < row; i++) {
            for(int j = 0; j < column; j++) {
                System.out.println("Input the element:");
                Mat[i][j] = in.nextInt();
                }
            }
        }   
    public void displayMatrix() {
        System.out.println("ORIGINAL MATRIX:");
        for(int[] i : Mat) {
            for(int j : i) {
                System.out.print(j+"  ");
                }
            System.out.println();
            }
        }   
    public void maxMin() {
        boolean flag = false;
        max = secondMax = Mat[0][0];
        for(int i = 0; i < row; i++) { // largest
            for(int j = 0; j < column; j++) {
                if(Mat[i][j] >= max ) {
                    max = Mat[i][j];
                    maxrowPosition = i+1;
                    maxcolPosition = j+1;
                    }
                }
            }
        for(int i = 0; i < row; i++) { // Second largest
            for(int j = 0; j < column; j++) {
                if(Mat[i][j] >= secondMax && Mat[i][j] < max ) {
                    flag = true;
                    secondMax = Mat[i][j];
                    secondMaxrowPosition = i+1;
                    secondMaxcolPosition = j+1;
                    }
                }
            }   
        if( flag == false) {
            secondMax = max;
            secondMaxrowPosition = maxrowPosition;
            secondMaxcolPosition = maxcolPosition;
            }
        System.out.println("The largest Number: "+max);
        System.out.println("Row: "+maxrowPosition);
        System.out.println("Coloumn: "+maxcolPosition);   
        System.out.println("The second largest Number: "+secondMax);
        System.out.println("Row: "+secondMaxrowPosition);
        System.out.println("Coloumn: "+secondMaxcolPosition);
        }
    public void rowSort() {   
        int[] temp = new int[row];
        int k = 0;
        System.out.println("SORTED LIST:");
        for(int i = 0; i < row; i++) {
            for(int j = 0; j < column; j++) {
                temp[k++] = Mat[i][j];
                }
            k = 0;
            Arrays.sort(temp);
            for( int l = 0; l < row ;l++)
                System.out.print(temp[l]+"  ");    
            System.out.println();
            }   
        }   
    private int[][] Mat;   
    private int row;
    private int column;
    private int max;
    private int secondMax;
    private int maxrowPosition;
    private int maxcolPosition;
    private int secondMaxrowPosition;
    private int secondMaxcolPosition;
    }   

No comments:

Post a Comment