
- Swift Tutorial
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Data Types
- Swift - Variables
- Swift - Optionals
- Swift - Tuples
- Swift - Constants
- Swift - Literals
- Swift - Operators
- Swift - Decision Making
- Swift - Loops
- Swift - Strings
- Swift - Characters
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift - Functions
- Swift - Closures
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift - Initialization
- Swift - Deinitialization
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Type Casting
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift Useful Resources
- Swift - Compile Online
- Swift - Quick Guide
- Swift - Useful Resources
- Swift - Discussion
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 (==).
- Related Articles
- Golang Program to Check Whether Two Matrices are Equal or Not
- Swift Program to Check if Two Arrays Are Equal or Not
- C program to compare if the two matrices are equal or not
- Check whether two schedules are view equal or not(DBMS)
- Golang Program to Check If Two Arrays are Equal or Not
- Swift Program to check whether a given string is Heterogram or not
- Java Program to check whether two Strings are an anagram or not.
- C# program to check whether two sequences are the same or not
- Program to check whether two sentences are similar or not in Python
- Swift Program to Check whether a number is a Perfect Cube or not
- Program to check whether two string arrays are equivalent or not in Python
- How to check if two matrices are equal in R?
- 8085 program to check whether both the nibbles of 8 bit number are equal or not
- Program to check whether we can make group of two partitions with equal sum or not in Python?
- Program to check whether leaves sequences are same of two leaves or not in python
