Program opened below. You can scroll through the algorithm, Java code and output.

Largest Elements and Sorted Matrix Rows

Download .java

Question

Input an n×n positive-integer matrix (n ≤ 20). Show the largest and second-largest distinct values with their one-based positions. Sort each row in ascending order.

Step 1: Start and read matrix order n. Step 2: Validate that n is between one and twenty. Step 3: Create the n by n integer matrix. Step 4: Prepare values and positions for the two largest distinct values. Step 5: Read each element using nested loops. Step 6: If a value exceeds the largest, move the old largest to second position. Step 7: Store the new largest value and its row-column location. Step 8: Otherwise update the second-largest value when it lies between the two. Step 9: Display the largest value and its one-based coordinates. Step 10: Display the second-largest distinct value when available. Step 11: Start insertion sort separately for every row. Step 12: Treat the next element as the key. Step 13: Shift larger preceding elements one place right. Step 14: Insert the key into the vacant location. Step 15: Display all rows of the sorted matrix. Step 16: Stop.
Java
import java.util.*;
class MatrixRank {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        if (n < 1 || n > 20) {
            System.out.println("Invalid order");
            return;
        }
        int[][] a = new int[n][n];
        int first = -1;
        int second = -1;
        int r1 = 0;
        int c1 = 0;
        int r2 = 0;
        int c2 = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                a[i][j] = sc.nextInt();
                int x = a[i][j];
                if (x > first) {
                    second = first;
                    r2 = r1;
                    c2 = c1;
                    first = x;
                    r1 = i;
                    c1 = j;
                } else if (x > second && x < first) {
                    second = x;
                    r2 = i;
                    c2 = j;
                }
            }
        }
        System.out.println("Largest: " + first + " at " + (r1 + 1) + "," + (c1 + 1));
        if (second >= 0) {
            System.out.println("Second: " + second + " at " + (r2 + 1) + "," + (c2 + 1));
        }
        for (int i = 0; i < n; i++) {
            for (int j = 1; j < n; j++) {
                int key = a[i][j];
                int k = j - 1;
                while (k >= 0 && a[i][k] > key) {
                    a[i][k + 1] = a[i][k];
                    k--;
                }
                a[i][k + 1] = key;
            }
        }
        for (int[] row : a) {
            for (int x : row) {
                System.out.print(x + " ");
            }
            System.out.println();
        }
    }
}
Input: 3 / 5 1 3 / 7 4 6 / 9 8 2 Output: Largest 9 at 3,1; Second 8 at 3,2 Sorted: 1 3 5 / 4 6 7 / 2 8 9
← Previous Program 7 of 84 Next →

Share a comment or request a program

Use this form to send a comment, report an issue, or suggest a new practical. Your message will be saved for review by the school.