# Swift Program to Multiply two Matrices Using Multi-dimensional Arrays

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

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 multiply two matrices, we multiply the mth row of the first matrix by an nth column of the second matrix and add the products. This will create an element at the mth row and nth columns of the resultant 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 product = Matrix 1 * Matrix 2

$\mathrm{\begin{bmatrix}(2^{*}4+3^{*}1+4^{*}4) & (2^{*}7+3^{*}1+4^{*}7) & (2^{*}1+3^{*}4+4^{*}2) \newline(5^{*}4+2^{*}1+7^{*}4) & (5^{*}7+2^{*}1+7^{*}7) & (5^{*}1+2^{*}4+7^{*}2) \newline(9^{*}4+3^{*}1+2^{*}4) & (9^{*}7+3^{*}1+2^{*}7) & (9^{*}1+3^{*}4+2^{*}2)\end{bmatrix}}$

$\mathrm{\begin{bmatrix}27 & 45 & 22 \newline50 & 86 & 27 \newline47 & 80 & 25\end{bmatrix}}$

## Algorithm

Step 1 − Define the size of the rows and columns.

Step 2 − Create two matrices of the same rows and columns using multidimensional arrays.

Step 3 − Create an empty matrix with a same number of rows and columns.

Step 4 − Run nested for loop to iterate through each element of both matrices.

Step 5 − Multiple the element at [x][z] position of matrix1 with each element of the row of matrix2 and add the values, store the values at [x][y] position of the resultant matrix. This process will continue until the last element of the matrix1

Step 6 − Print the resultant matrix.

## Example

Following the Swift program to multiply two matrices using multidimensional arrays.

import Foundation
import Glibc

// Size of the matrix
var row = 4
var col = 4

// Creating 4x4 matrix of integer type
var matrix1 : [[Int]] = [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]]
print("Matrix 1:")
for x in 0..<row {
for y in 0..<col {
print(matrix1[x][y], terminator:" ")
}
print("\n")
}
var matrix2 : [[Int]] = [[1, 0, 0, 1], [2, 0, 0, 2], [3, 0, 0, 3], [4, 0, 0, 4]]
print("Matrix 2:")
for x in 0..<row {
for y in 0..<col {
print(matrix2[x][y], terminator:" ")
}
print("\n")
}

// Creating 4x4 matrix to store the result
var Mul = Array(repeating: Array(repeating: 0, count: 4), count: 4)

// Multiply two matrices

// Using * operator
for x in 0..<row {
for y in 0..<col {
for z in 0..<row {
Mul[x][y] += matrix1[x][z] * matrix1[z][y]
}
}
}
print("Resultant matrix:")
for x in 0..<row {
for y in 0..<col {
print(Mul[x][y], terminator:" ")
}
print("\n")
}


## Output

Matrix 1:
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4

Matrix 2:
1 0 0 1
2 0 0 2
3 0 0 3
4 0 0 4

Resultant matrix:
10 10 10 10
20 20 20 20
30 30 30 30
40 40 40 40


Here in the above code, we create two 4x4 matrices along with values and one empty 4x4 matrix to store the result using multi-dimensional arrays. Now we run nested for loop to iterate through each element of both the matrices. Now we multiply the element of matrix1 at [x][z] position with each element of the row of the matrix2 using the * operator and add the values and store the result at [x][y] position of the resultant matrix. Repeat this process for all the elements of matrix1.

## Conclusion

Therefore, this is how we can multiply the matrix using multi-dimensional arrays. You can also create any size of matrices like 4x4, 6x3, and 2x3 using a multi-dimensional array and can able to perform multiplication on them.