Code with Vinay

ISC practical papers › ISC 2008 › Question 3

Matrix:Largest and Second Largest Element

ISC 2008 Computer Science Practical, Question 3 · Arrays and matrices

The question

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 tasks:

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 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

Question: ISC 2008 Computer Science Paper 2 (Practical), Question 3, set by the Council for the Indian School Certificate Examinations (CISCE). Solution and video: Vinay Singh.

Java solution

ISC2008Q3.java Download GitHub
import java.util.*;

class Matrix {
    private int list[][], n, largest, secondLargest;
    String largestLocation, secondLargestLocation;

    public Matrix(int n) {
        this.n = n;
        list = new int[n][n];
    }

    public void input() {
        Scanner in = new Scanner(System.in);
        System.out.println("List [] []");
        for (int row = 0; row < n; row++) {
            for (int col = 0; col < n; col++) {
                list[row][col] = in.nextInt();
            }
        }
        in.close();
        compute();
    }

    public void compute() {
        largest = secondLargest = Integer.MIN_VALUE;
        largestLocation = secondLargestLocation = "";
        for (int row = 0; row < n; row++) {
            for (int col = 0; col < n; col++) {
                if (list[row][col] > largest) {
                    secondLargest = largest;
                    secondLargestLocation = largestLocation;
                    largest = list[row][col];
                    largestLocation = "row " + (row + 1) + " and column is " + (col + 1);
                } else if (list[row][col] > secondLargest && list[row][col] != largest) {
                    secondLargest = list[row][col];
                    secondLargestLocation = "row " + (row + 1) + " and column is " + (col + 1);
                } else if (list[row][col] == largest) {
                    largestLocation += ", row " + (row + 1) + " and column is " + (col + 1);
                } else if (list[row][col] == secondLargest) {
                    secondLargestLocation += ", row " + (row + 1) + " and column is " + (col + 1);
                }
            }
        }
    }

    public void largestDetails() {
        System.out.println("The largest element " + largest + " is in " + largestLocation);
    }

    public void secondLargestDetails() {
        if (secondLargest != Integer.MIN_VALUE) {
            System.out.println("The second largest element " + secondLargest + " is in " + secondLargestLocation);
        }else{
            System.out.println("No second largest element");
        }
    }
    public void sortRows(){
        for( int row=0; row<n; row++){
            Arrays.sort(list[row]);
        }
    }
    public String toString(){
        String output="";
        for (int row = 0; row < n; row++) {
            for (int col = 0; col < n; col++) {
                output+=list[row][col];
                if(col<n-1) output+="\t";
            }
            output+="\n";
        }
        return output;
    }
}

class ISC2008Q3 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("INPUT:\tN = ");
        int n = in.nextInt();
        Matrix obj = new Matrix(n);
        obj.input();
        System.out.println("OUTPUT:");
        System.out.println(obj);
        obj.largestDetails();
        obj.secondLargestDetails();
        obj.sortRows();
        System.out.println("Sorted list");
        System.out.println(obj);
        in.close();
    }
}

All these solutions in one free book

Pragmatic Solutions to ISC Computer Science Practical Problems collects every program from 2004 to 2021 in a 202-page PDF.

Get the free PDF ₹0 ISC 2027 mock papers