
- 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 print the left diagonal matrix
In this article, we will learn how to write a swift program to print the left diagonal matrix.
A matrix is an arrangement of numbers in rows and columns. Matrix can be of various type like square matrix, horizontal matrix, vertical matrix etc. So here we print the left diagonal of square matrix. Square matrix is a matrix in which the number of rows and columns are same. For example 3x3, 5x5, 7x7, etc.
Algorithm
Step 1 − Create a function.
Step 2 − Run for-in loop to iterate through each element of the matrix.
Step 3 − Check if the row and column indices are the same.
Step 4 − If the element are diagonal elements, then print 2. Otherwise. Print 0.
Step 5 − Call the function and pass the matrix size to it.
Example
Following Swift program to print the left diagonal matrix.
import Foundation import Glibc // Function to print the left diagonal matrix func printLeftDiagonal(S: Int) { for x in 0..<S { for y in 0..<S { if x == y { print("2", terminator: " ") } else { print("0", terminator: " ") } } print() } } // Calling the function and passing // the size of the square matrix printLeftDiagonal(S: 5)
Output
2 0 0 0 0 0 2 0 0 0 0 0 2 0 0 0 0 0 2 0 0 0 0 0 2
Conclusion
Here in the above code, we create a function to print the left diagonal square matrix. As we know the that the size of rows and columns are same, so in our case the size is 4, means number of rows = 4 and number of columns = 4. So in this function, we use nested for-in loop which iterates through each row and column. Then check if the row and column indices are same that is for the diagonal elements. If yes then this function print 2. Otherwise print 0. So this is how we can print the left diagonal matrix. Here this method works only for square matrix not for other matrix like 4x5, 6x8, etc.
- Related Articles
- Swift Program to print the right diagonal matrix
- Swift Program to Print Diagonal Matrix Pattern
- Golang program to print the left diagonal matrix
- Swift Program to calculate the sum of left diagonal of the matrix
- Golang program to print right diagonal matrix
- Program to print a matrix in Diagonal Pattern.
- Golang program to calculate the sum of left diagonal matrix
- Swift program to Print an Identity Matrix
- Swift Program to calculate the sum of right diagonal of the matrix
- Swift Program to Print Left Triangle Star Pattern
- Python Program to calculate the sum of left diagonal of the matrix
- Print numbers in matrix diagonal pattern in C Program.
- Print matrix in diagonal pattern
- Swift Program to Print Boundary Elements of a Matrix
- Swift Program to Print Left Triangle Pattern of Numbers
