- 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 Calculate the Sum of Matrix Elements

A matrix is a rectangular array of numbers. Or we can say that 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.

In this article, we will learn how to write a swift program to calculate the sum of matrix elements. So here, using the following methods we calculate the sum of matrix elements.

Sum of square matrix elements

Sum of any matrix elements

For example, we have the following matrix −

$\mathrm{Matrix\:=\:\begin{bmatrix}1 & 4 & 5 & 6\newline4 & 7 & 8 & 9\newline3 & 1 & 1 & 1\newline9 & 3 & 5 & 5\end{bmatrix}}$

So the sum of the elements is = 72

## Method 1: Sum of Square Matrix Elements

So here, we calculate the sum of the elements of the 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** − Create a variable named sum to store the sum. The initial value of sum = 0.

**Step 3** − Run nested for-in loop to iterate through each row and column.

**Step 4** − In this nested loop add all the square matrix elements together and store the result into sum variable.

**Step 5** − Return sum.

**Step 6** − Create a square matrix and pass it in the function.

**Step 7** − Print the output.

### Example

Following Swift program to calculate the sum of matrix elements.

import Foundation import Glibc var mSize : Int = 5 // Function to find the sum of the elements of a square matrix func sumElements(matrix: [[Int]]) -> Int { var sum = 0 for R in 0..<mSize { for C in 0..<mSize { sum += matrix[R][C] } } return sum } let M = [[1, 2, 3, 5, 2], [4, 5, 6, 3, 4], [1, 8, 9, 4, 3], [4, 8, 1, 1, 1], [3, 8, 3, 4, 3]] print("Matrix is:") for x in 0..<mSize { for y in 0..<mSize { print(M[x][y], terminator:" ") } print("\n") } let res = sumElements(matrix: M) print("The Sum of the elements is:", res)

### Output

Matrix is: 1 2 3 5 2 4 5 6 3 4 1 8 9 4 3 4 8 1 1 1 3 8 3 4 3 The Sum of the elements is: 96

Here in the above code, we have a 5x5 square matrix and pass it in the function named sumElements() to find the sum of the elements of the matrix. In this function, we use nested for loop to iterate through each element of the input matrix and then add them together and store the result into sum variable. After finishing the nested for loop this function return the sum of all the elements that is 96 (according to the input matrix).

## Method 2: Sum of Any Type of Matrix Elements

So here we find the sum of the elements of any type of matrix. For example 2x3, 6x3, 4x5, etc.

### Algorithm

**Step 1** − Create a function.

**Step 2** − Create a variable named sum to store the sum. The initial value of sum = 0.

**Step 3** − Find the number of rows and columns.

**Step 4** − Run nested for-in loop to iterate through each row and column.

**Step 5** − In this nested loop, add all the elements together and store the result into sum variable.

**Step 6** − Return sum.

**Step 7** − Create a matrix and pass it in the function.

**Step 8** − Print the output

### Example

Following Swift program to calculate the sum of matrix elements.

import Foundation import Glibc // Function to find the sum of the elements of any type of matrix func matrixElementsSum(matrix: [[Int]]) -> Int { var sum = 0 let noRow = matrix.count let noColumn = matrix[0].count for R in 0..<noRow { for C in 0..<noColumn { sum += matrix[R][C] } } return sum } // Input 3x4 matrix let M = [[1, 2, 2, 2], [1, 1, 6, 1], [7, 8, 3, 9]] print("Matrix is:") for x in 0..<M.count { for y in 0..<M[0].count { print(M[x][y], terminator:" ") } print("\n") } let res = matrixElementsSum(matrix: M) print("The Sum of the elements is:", res)

### Output

Matrix is: 1 2 2 2 1 1 6 1 7 8 3 9 The Sum of the elements is: 43

Here in the above code, we have a 3x4 square matrix and pass it in the function named matrixElementsSum() to find the sum of the elements of the matrix. In this function, we use nested for loop to iterate through each element of the input matrix and then add them together and store the result into sum variable. After finishing the nested for loop this function return the sum of all the elements that is 43 (according to the input matrix). Here, matrix.count represent the total number of rows and matrix[0].count represent the total number columns.

## Conclusion

So this is how we can calculate the sum of the elements of different types of matrices like 6x6, 7x9, 2x4, etc.

- Related Articles
- Swift Program to calculate the sum of columns of matrix elements
- Swift Program to calculate the sum of rows of matrix elements
- Swift Program to calculate the sum of right diagonal of the matrix
- Swift Program to calculate the sum of left diagonal of the matrix
- Go language Program to calculate the sum of matrix elements
- Golang Program to calculate the sum of rows of matrix elements
- Golang Program to calculate the sum of columns of matrix elements
- Swift Program to Find the Sum of the Boundary Elements of a Matrix
- Swift Program to Calculate the sum of Elements in a Given Array
- Swift Program to Calculate the Sum of Natural Numbers
- Swift Program to Compute the Sum of Diagonals of a Matrix
- Swift Program to Print Boundary Elements of a Matrix
- Golang program to calculate the sum of left diagonal matrix
- Python Program to calculate the sum of right diagonal the matrix
- Swift Program to calculate the sum of first N even numbers