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

In this tutorial, we will write a go language program to add two matrices. The difference between a single-dimension array and a multidimensional array is that the former holds an attribute while the latter holds another array on the index. Additionally, every element of a multidimensional array will have the same data type.

## Adding Two Matrices Using Loops

Let us now look at a go language program to add two matrices using loops.

### Algorithm to the Above Program

Step 1 − Import the fmt package.

Step 2 − Now we need to start the main() function.

Step 3 − Then we are creating two matrices named matrixA and matrixB and store values in them.

Step 4 − Print the arrays on the screen using fmt.Println() function.

Step 5 − Initialize a new matrix of type int to hold the result.

Step 6 − To add the two matrices use the for loop to iterate over the two matrices

Step 7 − Using the first for loop is used to get the row of the matrix while the second for loop gives us the column of the matrix.

Step 8 − Once the loop gets over the new matrix has the sum of the two matrices.

Step 9 − Print the elements of the new matrix using for loops and fmt.Println() function.

### Example

package main
import (
"fmt"
)

// calling the main() function.
func main() {
var i, j int
var matrixC int
matrixA := int{
{0, 1},
{4, 5},
{8, 9},
}
matrixB := int{
{10, 11, 12},
{13, 14, 15},
{16, 17, 18},
}
fmt.Println("The first matrix is:")
for i = 0; i < 3; i++ {
for j = 0; j < 2; j++ {
fmt.Print(matrixA[i][j], "\t")
}
fmt.Println()
}
fmt.Println()

// printing the second matrix on the screen
fmt.Println("The second matrix is:")
for i = 0; i < 3; i++ {
for j = 0; j < 3; j++ {
fmt.Print(matrixB[i][j], "\t")
}
fmt.Println()
}
fmt.Println()
fmt.Println("The results of addition of matrix A & B: ")
for i = 0; i < 3; i++ {
for j = 0; j < 3; j++ {
matrixC[i][j] = matrixA[i][j] + matrixB[i][j]
}
}
for i = 0; i < 3; i++ {
for j = 0; j < 3; j++ {
fmt.Print(matrixC[i][j], "\t")
}
fmt.Println()
}
}


### Output

The first matrix is:
0	1
4	5
8	9
The second matrix is:
10	11	12
13	14      15
16	17	18

The results of addition of matrix A & B:
1012	12
17	19	15
24	26	18


## Add Two Matrices Using an External Function

In this example, we will use user-defined functions to add two matrices.

### Algorithm to the Above Program

Step 1 − Import the fmt package.

Step 2 − Create a function to add two matrices.

Step 3 − In this function use make() function to create a slice of the matrix and the range function to iterate over the matrix to find the sum

Step 4 − Start the main function.

Step 5 − Initialize two matrices and store elements to them print the matrices on the screen.

Step 6 − Call the AddMatrices() function by passing the two matrices as arguments to the function.

Step 7 − Store the result obtained and print it on the screen.

### Syntax

func make ([] type, size, capacity)


The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments.

func append(slice, element_1, element_2…, element_N) []T


The append function is used to add values to an array slice. It takes number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. The function then returns the final slice of array containing all the values.

### Example

package main
import (
"fmt"
)

// creating a function to add matrices
func AddMatrix(matrix1 int, matrix2 int) [][]int {
result := make([][]int, len(matrix1))
for i, a := range matrix1 {
for j, _ := range a {
result[i] = append(result[i], matrix1[i][j]+matrix2[i][j])
}
}
return result
}
func main() {
matrixA := int{
{0, 1, 2},
{4, 5, 6},
{8, 9, 10},
}
matrixB := int{
{10, 11},
{13, 14},
{16, 17},
}
fmt.Println("The first matrix is:")
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
fmt.Print(matrixA[i][j], "\t")
}
fmt.Println()
}
fmt.Println()

// printing the second matrix on the screen
fmt.Println("The second matrix is:")
for i := 0; i < 3; i++ {
for j := 0; j < 2; j++ {
fmt.Print(matrixB[i][j], "\t")
}
fmt.Println()
}
fmt.Println()

fmt.Println("The results of addition of matrix A & B: ")
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
fmt.Print(result[i][j], "\t")
}
fmt.Println()
}
}


### Output

The first matrix is:
0	1	2
4	5       6
8	9	10

The second matrix is:
10	11
13	14
16	17

The results of addition of matrix A & B:
10	12	2
17	19	6
24	26	10


## Conclusion

We have successfully compiled and executed a go language program to add to matrices along with examples. In the first example, we have implemented the logic in the main() function while in the second one we have used external functions to implement the above logic.

Updated on: 28-Dec-2022

341 Views 