Golang Program to Multiply two Matrices by Passing Matrix to a Function


In this tutorial, we will write a go language program to multiply two matrices by passing them to a function. In order to achieve this result, we will use both single dimension and multi-dimensional matrices. The difference between a single-dimension array and a multidimensional matrix is that the former has the same order while the latter has a different order of rows and columns.

Method 1: Multiply Two Matrices of the Same Order by Passing them to a Function

In this method, we will see to multiply two matrices of the same order bypassing the matrix to a user-defined function and then returning its output to the main() function.

Algorithm

Step 1 − Import the fmt package.

Step 2 − Create a function to multiply the given matrices called MultiplyMatrix().

Step 3 − This function uses three for loops. At every iteration of the matrix, we are updating the total variable by multiplying and adding the rows with columns of the two matrices.

Step 4 − After updating the total variable store the result at the respective place in the result variable reinitialize the total to zero and repeat the process.

Step 5 − Once all the iterations are complete return the result.

Step 6 − Now, start the main() function. Initialize two matrices of integer type and store values to them. Further, print these matrices on the screen.

Step 7 − Call the MultiplyMatrix() function by passing the two matrices as arguments to the function and storing the result.

Step 8 − Print the final result obtained on the screen using fmt.Println() function.

Example

Golang program to Multiply Two Matrices of Same Order.

package main
import (
   "fmt"
)

// creating a function to multiply matrices
func MultiplyMatrix(matrixA [3][3]int, matrixB [3][3]int) [3][3]int {
   var total int = 0
   var result [3][3]int

   // multiplying matrices and storing result
   for i := 0; i < 3; i++ {
      for j := 0; j < 3; j++ {
         for k := 0; k < 3; k++ {
            total = total + matrixA[i][k]*matrixB[k][j]
         }
         result[i][j] = total
         total = 0
      }
   }
   return result
}
func main() {
   
   // initializing variables
   var result [3][3]int
   var i, j 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()
   result = MultiplyMatrix(matrixA, matrixB)

   // printing final result
   fmt.Println("The results of multiplication 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 multiplication of matrix A & B:
45    48   51
201   216  231
357   384  411

Method 2: Multiply Two Matrices of Different Order by Passing them to a Function

In this method, we will write a program to multiply two matrices of different by passing the given matrices to a function.

Algorithm

Step 1 − Import the fmt package.

Step 2 − Create a function to multiply the given matrices called MultiplyMatrix().

Step 3 − This function uses three for loops. At every iteration of the matrix, we are updating the total variable by multiplying and adding the rows with columns of the two matrices.

Step 4 − After updating the total variable store the result at the respective place in the result, reinitialize the total to zero, and repeat the process.

Step 5 − Once all the iterations are complete return the result.

Step 6 − Now, start the main() function. Initialize two matrices of integer type and store values to them. Further, print these matrices on the screen.

Step 7 − Call the MultiplyMatrix() function by passing the two matrices as arguments to the function and storing the result.

Step 8 − Print the final result obtained using fmt.Println() function.

Example

Golang Program to multiply two matrices of different order by passing it to a function.

package main
import (
   "fmt"
)

// creating a function to multiply matrices
func MultiplyMatrix(matrixA [3][3]int, matrixB [3][2]int) [3][2]int {
   var total int = 0
   var result [3][2]int
   for i := 0; i < 3; i++ {
      for j := 0; j < 2; j++ {
         for k := 0; k < 3; k++ {
            total = total + matrixA[i][k]*matrixB[k][j]
         }
         result[i][j] = total
         total = 0
      }
   }
   return result
}
func main() {
   var result [3][2]int
   var i, j int
   matrixA := [3][3]int{
      {11, 12, 13},
      {4, 5, 6},
      {15, 16, 17},
   }
   matrixB := [3][2]int{
      {0, 4},
      {3, 6},
      {8, 9},
   }
   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 < 2; j++ {
         fmt.Print(matrixB[i][j], "\t")
      }
      fmt.Println()
   }
   fmt.Println()
   result = MultiplyMatrix(matrixA, matrixB)
   fmt.Println("The results of multiplication of matrix A & B: ")
   for i := 0; i < 3; i++ {
      for j := 0; j < 2; j++ {
         fmt.Print(result[i][j], "\t")
      }
      fmt.Println()
   }
}

Output

The first matrix is:
11  12  13
4   5   6
15  16  17

The second matrix is:
0  4
3  6
8  9

The results of multiplication of matrix A & B:
140   233
63    100
184   309

Conclusion

We have successfully compiled and executed a go language program to multiply two matrices by passing them to a function along with examples. In the first example, we have used two matrices of the same order while in the second one we are using matrices of a different order to achieve the result.

Updated on: 06-Jan-2023

355 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements