Swift Program to Find the Sum of the Boundary Elements of a Matrix


In this article, we will learn how to write a swift program to find the sum of the boundary elements of a matrix. Boundary elements of a matrix are those elements that are present on the boundary of the matrix that are elements in the first row, last row, first column, and last column. For example −

Given Matrix:
2 4 5 6 7 8
4 5 6 7 3 2
3 4 5 6 2 1
1 1 1 1 1 1 
3 4 3 4 3 4
2 2 2 2 2 2

Boundary elements are:
2 4 5 6 7 8
4         2
3         1
1         1
3         4        
2 2 2 2 2 2

Sum  = 2+4+5+6+7+8+2+1+1+4+2+2+2+2+2+2+3+1+3+4 = 63

Therefore, to find the sum of the boundary element, we iterate through each element of the given matrix, then check for the boundary elements, and then add them together to find their sum

Algorithm

  • Step 1 − Create a function.

  • Step 2 − Run a for loop to iterate through each element

  • Step 3 − Check if the current element lies on the boundary or not.

if(x == 0 || y == 0 || x == row-1 || y == col-1)
  • Step 4 − If the condition is true then add the boundary elements to find their sum

  • Step 5 − Create a matrix

  • Step 6 − Call the function and pass the matrix into it.

  • Step 7 − Print the output

Example 1

Following Swift program to find the sum of the boundary elements of a matrix.

import Foundation
import Glibc

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

// Function to print the sum of the boundary 
// elements of a matrix
func displayBoundary(mxt:[[Int]]) ->Int{
   var Bsum = 0
   for x in 0..<row{
      for y in 0..<col{
         if (x == 0){
            Bsum += mxt[x][y]
         }
         else if(x == row-1){
            Bsum += mxt[x][y]
         }
         else if(y == 0){
            Bsum += mxt[x][y]
         }
         else if (y == col-1){
            Bsum += mxt[x][y]
         }
      }
   }
   return Bsum
}
// Creating 5x5 matrix of integer type
var matrix : [[Int]] = [[1, 3, 4, 5, 2], [2, 16, 7, 5, 7], 
   [1, 0, 3, 1, 4], [2, 4, 3, 2, 4],
   [5, 2, 0, 0, 4]]

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

// Calling the function
print("Sum of the boundary elements of the matrix is", displayBoundary(mxt:matrix))

Output

Original Matrix:
1 3 4 5 2 

2 16 7 5 7 

1 0 3 1 4 

2 4 3 2 4 

5 2 0 0 4 

Sum of the boundary elements of the matrix is 46

Here in the above code, we have a 5x5 matrix. Now we create a function to find the sum of the boundary elements of the given matrix. In this function, we use nested for loop to iterate through each element of the array and check the element is present on the boundary or not using these conditions: x == 0, x == row-1, y == 0, and y == col-1. If these conditions are true then add the boundary elements together to find their sum.

Example 2

Following the Swift program to print boundary elements of a matrix.

import Foundation
import Glibc

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

// Function to find the sum of the boundary elements of a matrix
func SumBoundary(mxt:[[Int]])->Int{
   var Bsum = 0
   for x in 0..<row{
      for y in 0..<col{
      
         // Checking for the boundary elements
         if (x == 0 || y == 0 || x == row-1 || y == col-1){
         
            // Calculating the sum of the
            // boundary elements
            Bsum += mxt[x][y]
         }
      }
   }
   return Bsum
}

// Creating 5x4 matrix of integer type
var matrix : [[Int]] = [[1, 3, 4, 5], [2, 16, 7, 5], 
   [1, 0, 3, 1], [2, 4, 3, 2],
   [5, 2, 0, 0]]

print("Original Matrix:")
for x in 0..<row{
   for y in 0..<col{
      print(matrix[x][y], terminator:" ")
   }
   print("\n")
}
print("Sum of the boundary elements of the matrix is:", SumBoundary(mxt:matrix))

Output

Original Matrix:
1 3 4 5 

2 16 7 5 

1 0 3 1 

2 4 3 2 

5 2 0 0 

Sum of the boundary elements of the matrix is: 33

Here in the above code, we have a 4x5 matrix. Now we create a function to find the sum of the boundary elements of an array. In this function, we use nested for loop to iterate through each element of the array and check whether the element is present on the boundary or not using this condition: (x == 0 || y == 0 || x == row-1 || y == col-1). If these conditions are true, then add the boundary elements together to find the sum.

Conclusion

So by checking each and every element of the given matrix for boundary elements and then adding them together we can find the sum of the boundary elements of a matrix.

Updated on: 29-Dec-2022

93 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements