- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Swift Program to Compute the Sum of Diagonals of a Matrix
In this article, we will learn how to write a swift program to compute the sum of diagonals of a matrix. Every matrix has two diagonals which are known as primary and secondary diagonals. For example, we have the following 5x5 square matrix −
2 3 4 5 6 4 6 7 8 9 1 1 1 3 4 4 0 4 0 4 0 0 1 1 1
So the primary diagonal is formed by using elements 2, 6, 1, 0, 1 and the secondary diagonal is formed by using elements 6, 8, 1, 0, 0. Hence the sum of both diagonals are −
Primary diagonal = 2+6+1+0+1 = 10 Secondary diagonal = 6+8+1+0+0 = 15
Here we use the following methods −
Using for loop
Using nested for-loop
Method 1: Using For Loop
To calculate the sum of both diagonals of a matrix we can use a single for loop.
Algorithm
Step 1 − Create a function.
Step 2 − In this function, run a for loop to iterate through each element of the given matrix
Step 3 − Find the sum of the primary diagonal −
leftDiagonal += arr[x][x]
Step 4 − Find the sum of the secondary diagonal −
rightDiagonal += arr[x][size - x - 1]
Step 5 − Create a matrix.
Step 6 − Call the function and pass the matrix into it.
Step 7 − Print the output.
Example
Following Swift program to compute the sum of diagonals of a matrix using for loop.
import Foundation import Glibc // Size of the array var size = 4 // Function to find the sum of the diagonals func diagonalSum(arr:[[Int]]){ var leftDiagonal = 0 var rightDiagonal = 0 for x in 0..<size{ // Find the sum of left diagonal leftDiagonal += arr[x][x] // Find the sum of left diagonal rightDiagonal += arr[x][size - x - 1] } print("Sum of left diagonal", leftDiagonal) print("Sum of right diagonal", rightDiagonal) } // Creating 4x4 matrix of integer type var myArray : [[Int]] = [[2, 3, 4, 3], [5, 6, 7, 1], [8, 3, 2, 6], [4, 6, 4, 2]] print("Array:") for x in 0..<size{ for y in 0..<size{ print(myArray[x][y], terminator:" ") } print("\n") } diagonalSum(arr:myArray)
Output
Array: 2 3 4 3 5 6 7 1 8 3 2 6 4 6 4 2 Sum of left diagonal 12 Sum of right diagonal 17
Here in the above code, we have a matrix of integer type. Now we create a function to find the sum of the two diagonals of the given matrix. In this function, we run a for loop to iterate through each element of the given matrix and then find the sum of the primary and secondary diagonal.
Method 2: Using Nested For Loop
We can also find the sum of primary and secondary diagonals of the matrix using nested for loop.
Algorithm
Step 1 − Create a function.
Step 2 − In this function, run a nested for loop to iterate through each row and columns of the given matrix.
Step 3 − Find the primary diagonal and then add its elements −
if (x == y){ leftDiagonal += mxt[x][y] }
Step 4 − Find the secondary diagonal and then add its elements −
if ((x + y) == (size - 1)){ rightDiagonal += mxt[x][y] }
Step 5 − Create a matrix.
Step 6 − Call the function and pass the matrix into it.
Step 7 − Print the output.
Example
Following Swift program to compute the sum of diagonals of a matrix using nested for loop.
import Foundation import Glibc // Size of the array var size = 3 // Function to find the sum of the diagonals func diagonalSum(mxt:[[Int]]){ var leftDiagonal = 0 var rightDiagonal = 0 for x in 0..<size{ for y in 0..<size{ // Find the sum of left diagonal if (x == y) { leftDiagonal += mxt[x][y] } // Find the sum of left diagonal if ((x + y) == (size - 1)) { rightDiagonal += mxt[x][y] } } } print("Sum of left diagonal:", leftDiagonal) print("Sum of right diagonal:", rightDiagonal) } // Creating 3x3 matrix of integer type var matrix : [[Int]] = [[1, 3, 40], [2, 16, 7], [10, 3, 12]] print("Matrix:") for x in 0..<size{ for y in 0..<size{ print(matrix[x][y], terminator:" ") } print("\n") } // Calling the function diagonalSum(mxt:matrix)
Output
Matrix: 1 3 40 2 16 7 10 3 12 Sum of left diagonal: 29 Sum of right diagonal: 66
Here in the above code, we have a 3x3 matrix of integer type. Now we create a function to find the sum of the two diagonals of the given matrix. In this function, we run a nested loop to iterate through each row and column of the given matrix. Now we find the primary diagonal using the row-column condition− row = column. And then add the elements of the primary diagonal. Find the secondary diagonal using the row−column condition: row = noOfRows − column −1. And then add the elements of the secondary diagonal.
Conclusion
The square matrices has two diagonals primary(right to left) and secondary(left to right) diagonals of equal length. So either using nested or single for loop we can compute the sum of the diagonals of a matrix.