Golang Program to calculate the sum of rows of matrix elements



A matrix is a collection of numbers arranged in rows and columns, a two-dimensional array. Here we will use three methods to find the sum of elements and compare each method for the same using Go Programming language.

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 matrix.

  • 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 row 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

In this Golang program, 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()
   for i := 0; i < len(matrix); i++ {
      sum := 0
      for j := 0; j < len(matrix[i]); j++ {
         sum += matrix[i][j]
      }
      fmt.Printf("Sum of elements in row %d is %d\n", i+1, sum)
   }
}

Output

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

Sum of elements in row 1 is 3
Sum of elements in row 2 is 15
Sum of elements in row 3 is 27

Example 2

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

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()
   for i, row := range matrix {
      sum := 0
      for _, val := range row {
         sum += val
      }
      fmt.Printf("Sum of elements in row %d is %d\n", i+1, sum)
   }
}

Output

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

Sum of elements in row 1 is 23
Sum of elements in row 2 is 15
Sum of elements in row 3 is 27

Example 3

In this example, we will use the recursion approach to find the sum of rows of matrix elements.

package main
import "fmt"
func rowSum(matrix [][]int, row int) int {
   if row == len(matrix) {
      return 0
   }
   sum := 0
   for _, val := range matrix[row] {
      sum += val
   }
   return sum
}
func main() {
   matrix := [][]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()
   for i := 0; i < len(matrix); i++ {
      fmt.Printf("Sum of elements in row %d is %d\n", i+1, rowSum(matrix, i))
   }
}

Output

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

Sum of elements in row 1 is 23
Sum of elements in row 2 is 60
Sum of elements in row 3 is 27

Conclusion

We have successfully compiled and executed a go language program to add to matrices along with examples. In the first and second example we have used for loop and range functions respectively in the main() section of the program while in the last example we have used a separate function to implement the logic.


Advertisements