# Swift Program to Print Boundary Elements of a Matrix

In this article, we will learn how to write a swift program to print 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


So to find the boundary element, we iterate through each element of the given matrix and then check the given element lies on the boundary or not. If the element lies on the boundary then print the element, Otherwise print space.

## 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 the condition is true, print the element otherwise print whitespace.

if(x == 0 || y == 0 || x == row-1 || y == col-1)

• Step 4 − Create a matrix.

• Step 5 − Call the function and pass the matrix into it

• Step 6 − Print the output.

## Example 1

Following Swift program to print boundary elements of a matrix.

import Foundation
import Glibc

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

// Function to print the boundary elements of a matrix
func displayBoundary(mxt:[[Int]]){
for x in 0..<row{
for y in 0..<col{
if (x == 0){
print(mxt[x][y], terminator: " ")
}
else if(x == row-1){
print(mxt[x][y], terminator: " ")
}
else if(y == 0){
print(mxt[x][y], terminator: " ")
}
else if (y == col-1){
print(mxt[x][y], terminator: " ")
}
else{
print(" ", terminator: " ")
}
}
print()
}

}

// 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")
}
print("Boundary elements of the matrix:")

// Calling the function
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

Boundary elements of the matrix:
1 3 4 5 2
2       7
1       4
2       4
5 2 0 0 4


Here in the above code, we have a 5x5 matrix. Now we create a function to display 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 these conditions: x == 0, x == row-1, y == 0, and y == col-1. If these conditions are true then print the element otherwise print whitespace.

## Example 2

Following 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 print the boundary elements of a matrix
func displayBoundary(mxt:[[Int]]){
for x in 0..<row{
for y in 0..<col{
if (x == 0 || y == 0 || x == row-1 || y == col-1){
print(mxt[x][y], terminator:" ")
} else {
print(" ", terminator: " ")
}
}
print()
}
}

// 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("Boundary elements of the matrix:")

// Calling the function
displayBoundary(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

Boundary elements of the matrix:
1 3 4 5
2     5
1     1
2     2
5 2 0 0


Here in the above code, we have a 4x5 matrix. Now we create a function to display 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 print the element otherwise print whitespace.

## Conclusion

So by checking each and every element of the given matrix for boundary we can print boundary elements of a matrix.

Updated on: 29-Dec-2022

159 Views