# Swift Program to Add Two Matrix Using Multi-dimensional Arrays

In this article, we will learn how to write a swift program to add two matrices using a multi-dimensional array.

A matrix is a mathematical structure in which the elements are present in rows and columns format. For example, the first element is present at the a00 location, the second at a01, and so on. Therefore, to add two matrices we are going to use + operator to add the elements of two matrices like a00 + b00 and then store the sum into a new matrix. For example:

Matrix 1 −

$\mathrm{\begin{bmatrix}2 & 3 & 4 \newline5 & 2 & 7 \newline9 & 3 & 2\end{bmatrix}}$

Matrix 2 −

$\mathrm{\begin{bmatrix}4 & 7 & 1 \newline1 & 1 & 4 \newline5 & 7 & 2\end{bmatrix}}$

So the sum = Matrix 1 + Matrix 2

$\mathrm{Sum\:=\:\begin{bmatrix}2\:+\:4 & 3\:+\:7 & 4\:+\:1 \newline5\:+\:1 & 2\:+\:1 & 7\:+\:4 \newline9\:+\:5 & 3\:+\:7 & 2\:+\:2\end{bmatrix}\:=\:\begin{bmatrix}6 & 10 & 5 \newline6 & 3 & 11 \newline14 & 10 & 4\end{bmatrix}}$

## Algorithm

Step 1 − Define the size of the matrix

Step 2 − Create a function to find addition.

Step 3 − Create a matrix to store the sum. Here, the size and type of the matrix should be same are the input matrices.

Step 4 − Run nested for loop to iterate through each rows and columns of the the given matrices and add two matrices and store the result into ADD matrix

Step 5 − Create two matrices of same type.

Step 6 − Call the function and pass these matrices as a parameter in it to find the sum

Step 7 − Print the output.

## Example

Following Swift program to add two matrices using multi-dimensional array.

import Foundation
import Glibc

// Size of the matrix
var row = 5
var col = 5
func summ(mxt1:[[Int]], mxt2:[[Int]])
{
var ADD = Array(repeating: Array(repeating: 0, count: 5), count: 5)
for x in 0..<row
{
for y in 0..<col
{
ADD[x][y] = mxt1[x][y] + mxt2[x][y]
}
}
print("Resultant matrix:")
for x in 0..<row
{
for y in 0..<col
{
print(ADD[x][y], terminator:" ")
}
print("\n")
}
}
var matrix1 : [[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("Matrix 1:")
for x in 0..<row
{
for y in 0..<col
{
print(matrix1[x][y], terminator:" ")
}
print("\n")
}
var matrix2 : [[Int]] = [[3, 3, 0, 1, 2], [2, 4, 7, 8, 7], [1, 1, 1, 1, 4], [1, 4, 3, 3, 4], [5, 2, 2, 2, 5]]
print("Matrix 2:")
for x in 0..<row
{
for y in 0..<col
{
print(matrix2[x][y], terminator:" ")
}
print("\n")
}
summ(mxt1:matrix1, mxt2:matrix2)


## Output

Matrix 1:
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

Matrix 2:
3 3 0 1 2
2 4 7 8 7
1 1 1 1 4
1 4 3 3 4
5 2 2 2 5

Resultant matrix:
4 6   4  6  4
4 10 14 13 14
2  6  4  2  8
3  8  6  5  8
10 4  3  5  9


Here in the above code, we have two matrices of integer type. Now we create a function to find the sum of the given two matrices. So for the sum, we add the elements of the given two matrices of the same position and store the result into a new matrix at the same position on which they are added like ADD[0][0] = mxt1[0][0] +mxt1[0][0], ADD[0][1] = mxt1[0][1] +mxt1[0][1], and so on using + operator

## Conclusion

Therefore, this is how we can create and add two matrices using a multidimensional array. You can also create any size of matrices like 4x4, 6x3, and 2x3 using a multi-dimensional array and can able to perform operations on them.

Updated on: 09-Jan-2023

248 Views

##### Kickstart Your Career

Get certified by completing the course

Advertisements