Golang Program to Multiply to Matrix Using Multi-Dimensional Arrays


In this tutorial, we will write a go language program to multiply two matrices. The difference between a single-dimensional array and a multi-dimensional 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.

Method 1: Multiply Two Matrices using Multi-Dimensional Arrays in the Main Function

In this method, we will write a golang program to multiply two Multi-dimensional matrices using for loops in the main() function.

Algorithm

Step 1 − Import the fmt package.

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

Step 3 − To multiply matrices use three for loops. At every iteration of the matrix update 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 − Print the final result obtained on the screen using fmt.Println() function.

Example

Golang program to multiply two matrices using multidimensional arrays.

package main
import "fmt"
func main() {
   
   // initializing variables
   var result [3][2]int
   var i, j, k, total int
   total = 0
   matrixA := [3][3]int{
      {0, 1, 2},
      {4, 5, 6},
      {8, 9, 10},
   }
   matrixB := [3][2]int{
      {10, 11},
      {13, 14},
      {16, 17},
   }
   
   // printing matrices on the screen
   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()
   }
   
   // printing a new line
   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()
   
   // multiplying matrices and storing result
   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
      }
   }
   
   // printing result on the screen
   fmt.Println("Results of matrix multiplication: ")
   for i = 0; i < 3; i++ {
      for j = 0; j < 2; j++ {
         fmt.Print(result[i][j], "\t")
      }
      fmt.Println()
   }
   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
Results of matrix multiplication:
45    48
201  216
357  384

Method 2: Multiply Two Matrices using Multi-Dimensional Arrays in an External Function

In this method, we will create a user-defined function to perform the multiplication process of two matrices. The function we create will take the respective matrices as arguments and after performing the multiplication, it will return the final matrix, which we can receive and print, on the screen.

Algorithm

Step 1 − Import the fmt package.

Step 2 − Create a function to multiply the given matrices called MultiplyMatrix(). This function accepts the two matrices as an argument and returns the final matrix as result.

Step 3 − This function uses three for loops to achieve the logic. 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 reinitiate 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 using multidimensional arrays through an external 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

   // multiplying matrices and storing result
   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() {
   
   // initializing variables
   var result [3][2]int
   var i, j int
   matrixA := [3][3]int{
      {0, 1, 2},
      {4, 5, 6},
      {8, 9, 10},
   }
   matrixB := [3][2]int{
      {10, 11},
      {13, 14},
      {16, 17},
   }

   // printing matrices on the screen
   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()
   }
   
   // printing a new line
   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:
0  1  2
4  5  6
8  9 10
The second matrix is:
10  11
13  14
16  17
The results of multiplication of matrix A & B:
45   48
201  216
357  384

Conclusion

We have successfully compiled and executed a golang program to multiply two matrices using multidimensional arrays along with examples. In the first example, we used for loop in the main() function to implement the logic while in the second one we used an external user-defined function.

Updated on: 06-Jan-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements