Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Lekciq;
- import java.util.Scanner;
- public class CompareMatrices {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- // Read the first matrix
- int[][] matrix1 = readMatrix(scanner);
- // Read the second matrix
- int[][] matrix2 = readMatrix(scanner);
- // Compare the matrices and print the result
- if (areMatricesEqual(matrix1, matrix2)) {
- System.out.println("equal");
- } else {
- System.out.println("not equal");
- }
- }
- // Method to read a matrix from the console
- private static int[][] readMatrix(Scanner scanner) {
- int rows = scanner.nextInt();
- int cols = scanner.nextInt();
- int[][] matrix = new int[rows][cols];
- // Read matrix elements
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- matrix[i][j] = scanner.nextInt();
- }
- }
- return matrix;
- }
- // Method to compare two matrices element by element
- private static boolean areMatricesEqual(int[][] matrix1, int[][] matrix2) {
- if (matrix1.length != matrix2.length || matrix1[0].length != matrix2[0].length) {
- return false;
- }
- for (int i = 0; i < matrix1.length; i++) {
- for (int j = 0; j < matrix1[0].length; j++) {
- if (matrix1[i][j] != matrix2[i][j]) {
- return false;
- }
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement