
- 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 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.
- Related Articles
- Java Program to display upper triangular matrix
- Golang Program To Display Upper Triangular Matrix
- Swift Program to Display Lower Triangular Matrix
- C# program to Illustrate Upper Triangular Matrix
- JavaScript Program to Check if Matrix is Upper Triangular
- Program to check if matrix is upper triangular in C++
- Program to print Lower triangular and Upper triangular matrix of an array in C
- How to replace upper triangular matrix with lower triangular matrix and vice versa in R?
- How to create an upper triangular matrix using vector elements in R?
- C# Program to Illustrate Lower Triangular Matrix
- Swift program to Print Upper Star Triangle Pattern
- Swift Program to Print Upper Binary Triangle Pattern
- Swift program to Print Upper Numeric Triangle Pattern
- How to find the row and column index for upper triangular matrix elements in R?
- Swift Program to Print Mirror Upper Star Triangle Pattern
