Swift Program to Display Upper Triangular Matrix


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

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

Upper Triangular Matrix

Here (1, 5, 6, 2, 2, 3) is the primary diagonal so the elements below the primary diagonal are 0. Therefore, to create an upper triangular matrix we require a square matrix and then we convert the element to zero where the column is smaller 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 less 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 upper triangular matrix.

import Foundation
import Glibc

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

// Function to print upper triangular matrix
func printUpperTriangle(mxt:[[Int]]){
   if (row != col){
      print("Matrix is not a square matrix!\nso please enter a square matrix")
   } else{
      print("Upper 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
printUpperTriangle(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 

Upper Triangular Matrix:
1 3 4 5 2 
0 6 7 5 7 
0 0 3 1 4 
0 0 0 2 4 
0 0 0 0 4 

Here in the above code, we have a 5x5 square matrix. Now we create a function to print upper triangular matrix. In this function, we first check 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 less than the row then we replace the given element with zero. Otherwise, we print the elements.

Conclusion

So this is how we can display the upper 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 upper triangular matrix.

Updated on: 29-Dec-2022

120 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements