Didart

Compare Matrices

Jan 3rd, 2023
769
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.04 KB | None | 0 0
  1. package MultidimensionalArrays2;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class CompareMatrices {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         String[] firstDimension = scanner.nextLine().split(" ");
  10.         int row = Integer.parseInt(firstDimension[0]);
  11.         int col = Integer.parseInt(firstDimension[1]);
  12.  
  13.         int[][] firstMatrix = new int[row][col];
  14.  
  15.         for (int rowFirst = 0; rowFirst < row; rowFirst++) {
  16.             String[] firstArray = scanner.nextLine().split(" ");
  17.             for (int colFirst = 0; colFirst < col; colFirst++) {
  18.                 firstMatrix[rowFirst][colFirst] = Integer.parseInt(firstArray[colFirst]);
  19.  
  20.             }
  21.         }
  22.  
  23.         String[] secondDimension = scanner.nextLine().split(" ");
  24.         int rowSecond = Integer.parseInt(secondDimension[0]);
  25.         int colSecond = Integer.parseInt(secondDimension[1]);
  26.  
  27.         int[][] secondMatrix = new int[rowSecond][colSecond];
  28.  
  29.         for (int rows = 0; rows < rowSecond; rows++) {
  30.             String[] secondArray = scanner.nextLine().split(" ");
  31.             for (int cols = 0; cols < colSecond; cols++) {
  32.                 secondMatrix[rows][cols] = Integer.parseInt(secondArray[cols]);
  33.  
  34.             }
  35.         }
  36.         if (EqualMatrix(firstMatrix, secondMatrix)) {
  37.             System.out.println("equal");
  38.  
  39.         } else {
  40.             System.out.println("not equal");
  41.         }
  42.  
  43.     }
  44.  
  45.     public static boolean EqualMatrix(int[][] firstMatrix, int[][] secondMatrix) {
  46.         if (firstMatrix.length != secondMatrix.length) {
  47.             return false;
  48.         }
  49.         for (int row = 0; row < firstMatrix.length; row++) {
  50.             if (firstMatrix[row].length != secondMatrix[row].length) {
  51.                 return false;
  52.             }
  53.             for (int col = 0; col < firstMatrix[row].length; col++) {
  54.                 if (firstMatrix[row][col] != secondMatrix[row][col]) {
  55.                     return false;
  56.                 }
  57.             }
  58.         }
  59.         return true;
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment