Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
4.7 kB
7
Indexable
Never
import java.util.Scanner;
import java.util.Random;

public class Matrix {
    public static void main(String[] args) {
        System.out.println("Vvedite razmernost' matrix");
        Scanner console = new Scanner(System.in);
        int m = console.nextInt();
        int n = console.nextInt();
        int matrix [][] = new int[m][n];
        System.out.println("How do you want to create Matrix? 0- random; 1-keyboard");
        Scanner choise1 = new Scanner(System.in);
        int choise = choise1.nextInt();
         switch (choise) {
            case 0:
                createOfMatrixRandom(matrix, n, m);
                break;
            case 1:
                createMatrixFromKeyboard(matrix, n, m);
                break;
            default:
                System.out.println("You wrote wrong, write 0 or 1");
                return;
        }
        
        System.out.println("What you want to do with matrix? 0- summ of elements; 1-transp, 2 - max of matrix, 3 - diagonal of matrix, 4 - average of matrix");
        Scanner choise2 = new Scanner(System.in);
        int action = choise2.nextInt();
        switch (action){
        case 0:
            System.out.print(summOfMatrix(matrix, n, m));
            return;
        case 1:
            System.out.print(transpOfMatrix(matrix, n, m));
            return;
        case 2:
            System.out.print(maxOfMatrix(matrix, n, m));
            return;
        case 3:
            System.out.print(diagonalOfMatrix(matrix, n, m));
            return;
        case 4: 
            System.out.print(averageOfMatrix(matrix));
            return;
        default:
            System.out.print("Dummy, you wrote wrong input, please write 0 or 1 or 2 or 3 or 4");
            return;
    }    
}
    
    public static void createMatrixFromKeyboard(int[][] matrix, int n, int m) {
        System.out.println("Enter the elements of the matrix:");
        Scanner elements = new Scanner(System.in);
        for (int i = 0; i < m; i++) {
           for (int j = 0; j < n; j++) {
                matrix[i][j] = elements.nextInt();
           }
        }
        printMatrix(matrix);
    }
    
    public static void createOfMatrixRandom(int [][] matrix, int n, int m) {
        System.out.print("Please write the distance of your matrix like first input - begging of range and second input as begging of range");
        
        Scanner range = new Scanner(System.in);
        Random random = new Random();
        int beg = range.nextInt();
        int end = range.nextInt();
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                matrix[i][j] = random.nextInt((end - beg) + 1) + beg; 
            }
        }
        printMatrix(matrix);
    }

public static int summOfMatrix(int [][] matrix, int n, int m) {
    int sum=0; 
    for(int i=0; i<m; i++){
        for (int j=0; j<n; j++){
            sum = sum + matrix [i][j];
        }
        }
        return sum;
    }

public static int transpOfMatrix(int [][] matrix, int n, int m) {
    int matrixtr [][] = new int[m][n];
    
    for(int i=0; i<m; i++){
        for (int j=0; j<n; j++){
            matrixtr[j][i]=matrix[i][j];
        }
        }
        printMatrix(matrixtr);
        return 999;
    }

public static int maxOfMatrix(int [][] matrix, int n, int m) {
    int max=Integer.MIN_VALUE; 
    for(int i=0; i<m; i++){
        for (int j=0; j<n; j++){
            if(max<matrix [i][j]) max=matrix [i][j];
        }
        }
        return max;
    }
    

public static int diagonalOfMatrix(int [][] matrix, int n, int m) {
    System.out.print("Main diagonal = 0, Subdiagonal = 1");
    Scanner diagonalScanner = new Scanner(System.in);
    int diagonal = diagonalScanner.nextInt();
    int sum = 0;
   if (diagonal == 0) {
            for(int i = 0; i < Math.min(m, n); i++) {
                sum += matrix[i][i];
            }
        } else if (diagonal == 1) {
            for(int i = 0; i < Math.min(m, n); i++) {
                sum += matrix[i][n - i - 1];
            }
        } else {
            System.out.println("Dummy, you wrote wrong input, please write 0 or 1");
            return -1;
        }
        return sum;
}

    public static double averageOfMatrix(int [][] matrix) {
        int m = matrix.length;
        int n = matrix[0].length;
        int sum = summOfMatrix(matrix, m, n);
        return (double) sum / (m * n);
    }


    public static void printMatrix(int[][] matrix) {
        for(int[] row : matrix) {
            for (int element : row) {
                System.out.print(element + " ");
            }
            System.out.println();
        }
}
}
Leave a Comment