- 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 rows of matrix elements

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 rows of matrix elements. Here we are using the following methods to find the sum of the rows elements −

Using nested for-in loop

Using in-built function

## Method 1: Using nested for-in loop

Here we use nested for-in loop to find the sum of rows of matrix elements.

### 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 elements row-wise and store the result into sum variable.

**Step 6** − After each row reset the value of sum = 0 for the sum of next row elements.

**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 rows of matrix elements.

import Foundation import Glibc // Function to find the sum of each row of a matrix func rowElementsSum(matrix: [[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] } print("Sum of row \(R) = \(sum)") // Reseting sum sum = 0 } } // Input 4x3 matrix let M = [[1, 2, 2], [1, 1, 6], [7, 8, 3], [2, 4, 2]] print("Matrix is:") for x in 0..<M.count { for y in 0..<M[0].count { print(M[x][y], terminator:" ") } print("\n") } // Calling function rowElementsSum(matrix: M)

### Output

Matrix is: 1 2 2 1 1 6 7 8 3 2 4 2 Sum of row 0 = 5 Sum of row 1 = 8 Sum of row 2 = 18 Sum of row 3 = 8

Here in the above code, we have a 4x3 matrix and then pass it in the function named rowElementsSum() to find the sum of the row’s elements of the matrix. In this function, we use nested for loop to iterate through each element of the input matrix and then add it to the corresponding row sum. After finishing the nested for loop this function return the row sums that is row 0 = 5, row 1 = 8, row 2 = 18 and row 3 = 8(according to the input matrix).

## Method 2: Using in-built function

To find the sum of the rows of the given matrix elements we uses reduce(_:_:) function. This function returns a result by combining the elements of the array or any sequence using the given closure.

### Syntax

func reduce(_initial: Value, _next: Value)

Here, initial represent the value to use as an initial accumulating value. It passes to the next for the first time the closure is executed. And next represent a closure that combines an accumulating value and an element of the array into a new accumulating value which if further used for next call of the next closure or return to the caller.

### Algorithm

**Step 1** − Create a matrix.

**Step 2** − Print the matrix.

**Step 3** − Create an 1-D array to store the sum of the rows.

**Step 4** − Run a for loop to iterate through each row.

**Step 5** − Find the sum of the rows elements using reduce() function.

let sum = row.reduce(0, +)

**Step 6** − Now store the sum of each row in the array using append() function.

RowsSum.append(sum)

**Step 7** − Print the resultant array.

### Example

Following Swift program to calculate the sum of rows of matrix elements.

import Foundation import Glibc // 5x3 matrix let matrix = [[1, 2, 3, 4, 6], [4, 5, 6, 1, 1], [7, 8, 9, 5, 5]] // Displaying matrix print("Matrix is:") for x in 0..<matrix.count { for y in 0..<matrix[0].count { print(matrix[x][y], terminator:" ") } print("\n") } // Calculating the sum of the rows elements of the matrix var RowsSum = [Int]() for row in matrix { let sum = row.reduce(0, +) RowsSum.append(sum) } print("So the sums of the individual rows are:", RowsSum)

### Output

Matrix is: 1 2 3 4 6 4 5 6 1 1 7 8 9 5 5 So the sums of the individual rows are: [16, 17, 34]

Here in the above code, we have a 5x3 matrix. Then create an empty 1-D array to store the sum of each row. The using for loop we iterate through each row of the given matrix and adds the elements of the row using reduce() function. This sum is then store in the RowsSum array. So according to the given matrix the final result is [16, 17, 34]. Here 16 is the sum of row 0 elements(1+2+3+4+6), 17 is the sum of row 1 elements(4+5+6+1+1) and so on.

## Conclusion

So this is how we can calculate the sum of rows of matrix elements. Here using the above methods we can calculate the sum of any type of matrix like 6x6, 9x9, 4x3, 6x7, etc.

- Related Articles
- Swift Program to Calculate the Sum of Matrix Elements
- Golang Program to calculate the sum of rows of matrix elements
- Swift Program to calculate the sum of columns 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 columns of matrix elements
- Swift Program to Find the Sum of the Boundary Elements of a Matrix
- Swift Program to Interchange Elements of First and Last Rows 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 left diagonal of the matrix