Swift Program to Check Whether Two Matrices Are Equal or Not


In this article, we will learn how to write a swift program to check whether two matrices are equal or not. Here, we create two matrices, and using the not equal to the operator (!=), we check whether all the elements of both matrices are equal or not.

Algorithm

  • Step 1 − Create a function.

  • Step 2 − Use nested for loop to iterate through each row and column.

  • Step 3 − Check if the element of matrix1 is not equal to matrix2. If the condition is true, then return 0, otherwise, return 1.

  • Step 4 − Create two matrices of the same data type.

  • Step 5 − Check if the values returned by the function is equal to 1. If the condition is true, then print “Both the matrices are the same”. Otherwise print “Matrices are not same”.

Example

Following Swift program to check whether two matrices are equal or not.

import Foundation
import Glibc

// Size of the matrix 
var row = 5
var col = 5

// Function to check if the given two matrices are equal or not
func checkMatrixEquality(mxt1:[[Int]], mxt2:[[Int]])->Int{
   for x in 0..<row{
      for y in 0..<col{
         if (mxt1[x][y] != mxt2[x][y]){
            return 0
         }
      }
   }
   return 1
}

// Creating 5x5 matrix of integer type
var matrix1 : [[Int]] = [[1, 3, 4, 5, 2], [2, 6, 7, 5, 7], 
   [1, 5, 3, 1, 4], [2, 4, 3, 2, 4],
   [5, 2, 1, 3, 4]]

print("Original Matrix 1:")
for x in 0..<row{
   for y in 0..<col{
      print(matrix1[x][y], terminator:" ")
   }
   print("\n")
}
var matrix2 : [[Int]] = [[1, 3, 4, 5, 2], [2, 6, 7, 5, 7], 
   [1, 5, 3, 1, 4], [2, 4, 3, 2, 4],
   [5, 2, 1, 3, 4]]

print("Original Matrix 2:")
for x in 0..<row{
   for y in 0..<col{
      print(matrix2[x][y], terminator:" ")
   }
   print("\n")
}

// Printing the result
if (checkMatrixEquality(mxt1:matrix1, mxt2:matrix2) == 1){
   print("Yes! Given two matrices are equal!")
} else{
   print("No! Given two matrices are not equal!")
}

Output

Original Matrix 1:
1 3 4 5 2 

2 6 7 5 7 

1 5 3 1 4 

2 4 3 2 4 

5 2 1 3 4 

Original Matrix 2:
1 3 4 5 2 

2 6 7 5 7 

1 5 3 1 4 

2 4 3 2 4 

5 2 1 3 4 

Yes! Given two matrices are equal!

Here in the above code, we have two integer-type matrices. Now we create a function to check if the given matrices are the same or not. In this method, we use nested for loop to iterate through each element of the given matrices and compare them together using the not equal to operator(!=). If the condition is true, then this method returns 1, otherwise, it will return 0. Now we call the function and check if the return value is equal to 1 using == operator. If the condition is true, then we get “Yes! Given two matrices are equal!” as an output. Otherwise, we get "No! Given two matrices are not equal!”.

Conclusion

So this is how we can check whether two matrices are equal or not using comparison operators that are not equal to (!=) and equal to (==).

Updated on: 29-Dec-2022

117 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements