Go language Program to calculate the sum of matrix elements


What is a Matrix?

A matrix is a collection of numbers that are arranged in rows and columns, which is a two-dimensional array. Here we will use three examples to find the sum of elements and compare each element of the matrix for the same using the Golang program.

Algorithm

  • Step 1 − Import the fmt package.

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

  • Step 3 − Then we are creating a matrix naming matrixA.

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

  • Step 5 − Initialize a new variable called sum of type int to hold the resultant sum.

  • Step 6 − To find sum of the elements use the for loop to iterate over the matrix.

  • 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 matrix elements update the sum variable by adding values to it.

  • Step 9 − Print the sum of the matrix on the screen.

Example 1

We will use a for loop to iterate over the matrix and find the sum of its elements and print it on the screen.

package main
import "fmt"
func main() {
   matrix := [3][3]int{
      {0, 1, 2},
      {4, 5, 6},
      {8, 9, 10},
   }
   fmt.Println("The given matrix is:")
   for i := 0; i < 3; i++ {
      for j := 0; j < 3; j++ {
         fmt.Print(matrix[i][j], "\t")
      }
      fmt.Println()
   }
   fmt.Println()
   var sum int = 0
   for i := 0; i < len(matrix); i++ {
      for j := 0; j < len(matrix[i]); j++ {
         sum += matrix[i][j]
      }
   }
   fmt.Println("Sum of matrix elements:", sum)
}

Output

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

Sum of matrix elements: 45

Example 2

In this example, we will find the sum of the matrix elements using range function in Go Programming.

package main
import "fmt"
func main() {
   matrix := [3][3]int{
      {20, 1, 2},
      {4, 5, 6},
      {8, 9, 10},
   }
   fmt.Println("The given matrix is:")
   for i := 0; i < 3; i++ {
      for j := 0; j < 3; j++ {
         fmt.Print(matrix[i][j], "\t")
      }
      fmt.Println()
   }
   fmt.Println()
   var sum int = 0
   for _, row := range matrix {
      for _, element := range row {
         sum += element
      }
   }
   fmt.Println("Sum of matrix elements:", sum)
}

Output

The given matrix is:
20	1	2	
4	5	6	
8	9	10	

Sum of matrix elements: 65

Example 3

In this example, we will use the recursion approach to find the sum of a matrix element using go programming

package main
import "fmt"
// creating a funciton to find sum through recursion
func sumMatrix(matrix [3][3]int, row int, col int) int {
   if row == len(matrix) {
      return 0
   }
   if col == len(matrix[row]) {
      return sumMatrix(matrix, row+1, 0)
   }
   return matrix[row][col] + sumMatrix(matrix, row, col+1)
}
func main() {
   matrix := [3][3]int{
      {20, 1, 2},
      {4, 50, 6},
      {8, 9, 10},
   }
   fmt.Println("The given matrix is:")
   for i := 0; i < 3; i++ {
      for j := 0; j < 3; j++ {
         fmt.Print(matrix[i][j], "\t")
      }
      fmt.Println()
   }
   fmt.Println()
   result := sumMatrix(matrix, 0, 0)
   fmt.Println("Sum of matrix elements:", result)
}

Output

The given matrix is:
20	1	2	
4	50	6	
8	9	10	

Sum of matrix elements: 110

Conclusion

We have successfully compiled and executed a go language program to add to matrices along with examples. We have used three methods here in the first two methods we are using the for loop and range function respectively to iterate over the matrix while in the last example we are using recursion approach to calculate sum. Note that the third method is more efficient in functionality and it takes less time to get executed.

Updated on: 16-Feb-2023

105 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements