Swift Program to Display Lower Triangular Matrix


In this article, we will learn how to write a swift program to display lower triangular matrix. Lower triangular matrix is a matrix in which all the elements above primary diagonal are zero. As shown in the below image:

$$\mathrm{\begin{bmatrix} 1 & 0 & 0 & 0 & 0 & 0\newline 3 & 5 & 0 & 0 & 0 & 0\newline 1 & 4 & 6 & 0 & 0 & 0\newline 1 & 2 & 2 & 2 & 0 & 0\newline 4 & 5 & 6 & 1 & 2 & 0\newline 1 & 1 & 1 & 4 & 5 & 3\newline \end{bmatrix}}$$

Lower triangular matrix

Here, (1, 5, 6, 2, 2, 3) is the primary diagonal so the elements above the primary diagonal are 0. Therefore, to create a lower triangular matrix we require a square matrix and then we convert the element to zero where the column is greater than the row.

Algorithm

  • Step 1 − Create a function.

  • Step 2 − In this function, first we check if the given matrix is a square or not

  • Step 3 − If the matrix is not a square matrix, then display a message.

  • Step 4 − If the matrix is a square matrix, then use a nested for loop to iterate through each row and column.

  • Step 5 − If the column position is greater than the row position, then replace the element with 0. Otherwise, print the elements.

  • Step 6 − Create a square matrix.

  • Step 7 − Call the function and pass the matrix into it as a parameter.

  • Step 8 − Print the output.

Example

Following Swift program to display lower triangular matrix.

import Foundation
import Glibc

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

// Function to print lower triangular matrix
func printLowerTriangle(mxt:[[Int]]){
   if (row != col){
      print("Matrix is not a square matrix!\nso please enter a square matrix")
   } else {
      print("Lower Triangular Matrix:")
      for x in 0..<row{
         for y in 0..<col{
            if(x < y){
               print("0", terminator:" ")
            } else {
               print(mxt[x][y], terminator: " ")
            }
         }
         print()
      }
   }
}

// Creating 5x5 matrix of integer type
var matrix : [[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:")
for x in 0..<row{
   for y in 0..<col{
      print(matrix[x][y], terminator:" ")
   }
   print("\n")
}

// Calling the function
printLowerTriangle(mxt:matrix)

Output

Original Matrix:
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 

Lower Triangular Matrix:
1 0 0 0 0 
2 6 0 0 0 
1 5 3 0 0 
2 4 3 2 0 
5 2 1 3 4 

Here in the above code, we have a 5x5 square matrix. Now we create a function to print a lower triangular matrix. In this function, we first check whether the given matrix is a square matrix or not. Here the matrix is a square matrix so now we find the upper triangular matrix by iterating through each row and column and if the column position is greater than the row then we replace the given element with zero. Otherwise, we print the elements.

Conclusion

So this is how we can display a lower triangular matrix. Or we can say by checking the position of the rows and columns( that is row<column) we convert the given square matrix into the lower triangular matrix.

Updated on: 29-Dec-2022

157 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements