Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang Program To Add Two Matrices
In this tutorial, we will write a go language program to add two matrices. A matrix is a collection of numbers that are arranged in rows and columns, which is a two-dimensional array.
Go Language Program To Add Two Matrices
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"
)
func main() {
var i, j int
var matrixC [3][3]int
matrixA := [3][3]int{
{0, 1, 2},
{4, 5, 6},
{8, 9, 10},
}
matrixB := [3][3]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 < 3; j++ {
fmt.Print(matrixA[i][j], "\t")
}
fmt.Println()
}
fmt.Println()
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 24 5 6 8 9 10 The second matrix is: 10 11 12 13 14 15 16 17 18 The results of addition of matrix A & B: 10 12 14 17 19 21 24 26 28
Golang Program To Add Two Matrices Using External Function
In this example, we will use user-defined functions to add two matrices.
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
Algorithm
Step 1 ? Import the fmt package.
Step 2 ? Create a function to add two matrices.
Step 3 ? In this function use the 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 in them and 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.
Example
package main
import (
"fmt"
)
func AddMatrix(matrix1 [3][3]int, matrix2 [3][3]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 := [3][3]int{
{0, 1, 2},
{4, 5, 6},
{8, 9, 10},
}
matrixB := [3][3]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 < 3; j++ {
fmt.Print(matrixA[i][j], "\t")
}
fmt.Println()
}
fmt.Println()
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()
result := AddMatrix(matrixA, matrixB)
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 12 13 14 15 16 17 18 The results of addition of matrix A & B: 10 12 14 17 19 21 24 26 28
Conclusion
We have successfully compiled and executed a go language program to add to matrices along with examples.