Q11

Pg 127
mail@pastecode.io avatar
unknown
java
a year ago
4.0 kB
2
Indexable
Never
import java.util.Scanner;

public class MatrixOperations {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the number of rows (M): ");
        int rows = scanner.nextInt();

        System.out.print("Enter the number of columns (N): ");
        int columns = scanner.nextInt();

        if (rows <= 2 || columns <= 2 || rows >= 20 || columns >= 20) {
            System.out.println("Invalid matrix dimensions. Rows and columns must be between 3 and 19.");
            return;
        }

        int[][] matrix = new int[rows][columns];

        System.out.println("Enter the elements of the matrix:");

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                System.out.print("Element [" + (i + 1) + "][" + (j + 1) + "]: ");
                matrix[i][j] = scanner.nextInt();
            }
        }

        System.out.println("\nInput Matrix:");
        displayMatrix(matrix);

        int[] max = findMaxValue(matrix);
        int[] min = findMinValue(matrix);

        System.out.println("\nMaximum value: " + max[0] + " at row " + max[1] + ", column " + max[2]);
        System.out.println("Minimum value: " + min[0] + " at row " + min[1] + ", column " + min[2]);

        sortMatrix(matrix);

        System.out.println("\nSorted Matrix:");
        displayMatrix(matrix);
    }

    public static void displayMatrix(int[][] matrix) {
        int rows = matrix.length;
        int columns = matrix[0].length;

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static int[] findMaxValue(int[][] matrix) {
        int max = matrix[0][0];
        int rows = matrix.length;
        int columns = matrix[0].length;

        int maxRow = 0;
        int maxColumn = 0;

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                if (matrix[i][j] > max) {
                    max = matrix[i][j];
                    maxRow = i;
                    maxColumn = j;
                }
            }
        }

        return new int[]{max, maxRow, maxColumn};
    }

    public static int[] findMinValue(int[][] matrix) {
        int min = matrix[0][0];
        int rows = matrix.length;
        int columns = matrix[0].length;

        int minRow = 0;
        int minColumn = 0;

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                if (matrix[i][j] < min) {
                    min = matrix[i][j];
                    minRow = i;
                    minColumn = j;
                }
            }
        }

        return new int[]{min, minRow, minColumn};
    }

    public static void sortMatrix(int[][] matrix) {
        int rows = matrix.length;
        int columns = matrix[0].length;

        int totalElements = rows * columns;

        // Flattening the matrix into a 1D array for sorting
        int[] flattenedMatrix = new int[totalElements];
        int index = 0;

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                flattenedMatrix[index++] = matrix[i][j];
            }
        }

        // Bubble sort
        for (int i = 0; i < totalElements - 1; i++) {
            for (int j = 0; j < totalElements - i - 1; j++) {
                if (flattenedMatrix[j] > flattenedMatrix[j + 1]) {
                    int temp = flattenedMatrix[j];
                    flattenedMatrix[j] = flattenedMatrix[j + 1];
                    flattenedMatrix[j + 1] = temp;
                }
            }
        }

        // Rearranging the sorted elements back into the matrix
        index = 0;

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                matrix[i][j] = flattenedMatrix[index++];
            }
        }
    }
}