Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Golang Program to Compute the Sum of Diagonals of a Matrix
In this tutorial, we will write a go language program to find the sum of diagonal elements of a matrix.
Compute the Sum of Diagonals of a Matrix
In this example, we will find the sum of left diagonal elements of a 3 x 3 matrix using external function.
Algorithm
Step 1 ? Import the fmt package.
Step 2 ? Create a function to find the sum of the given matrix.
Step 3 ? In this function initialize an integer variable named sum and use a for loop to intend over the matrix array.
Step 4 ? In each iteration update the sum variable with the diagonal elements of matrix (like 00, 11, 22) and return the sum variable.
Step 5 ? Start the main function. Here, first we need to initialize a matrix and print it on the screen using for loops.
Step 6 ? Now, call the matrixSum() by passing the matrix as argument.
Step 7 ? Store the result in result variable and print it on the screen using fmt.Println() function.
Example
package main
import "fmt"
// function to find sum
func matrixSum(mat [][]int, n int) int {
var sum int = 0
for k := range mat {
sum = sum + mat[k][k]
}
return sum
}
func main() {
mat := [][]int{
{10, 1, 2},
{4, 5, 6},
{8, 9, 10},
}
fmt.Println("The given matrix is: \n")
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
fmt.Print(mat[i][j], "\t")
}
fmt.Println()
}
fmt.Println()
var n int = 2
result := matrixSum(mat, n)
fmt.Println("The Sum of left diagonal element of Matrix = ", result)
}
Output
The given matrix is: 10 1 2 4 5 6 8 9 10 The Sum of left diagonal element of Matrix = 25
Find the Sum of the Right Diagonal of a 3 x 3 Matrix
In this example, we will write a go language program to find the sum of the right diagonal element of a 3 X 3 matrix.
Algorithm
Step 1 ? Import the fmt package.
Step 2 ? Create a function to find the sum of the given matrix.
Step 3 ? In this function initialize an integer variable named sum and used a for loops to intend over the matrix array.
Step 4 ? In each iteration update the sum variable with the diagonal elements of matrix (like 02, 11, 20) and return the sum variable.
Step 5 ? To obtain this we have used a if condition which satisfies the above condition by computing the sum of iterated variables to 2.
Step 6 ? Start the main function. Here, first we need to initialize a matrix and print it on the screen using for loops.
Step 7 ? Now, call the matrixSum() by passing the matrix as argument.
Step 8 ? Store the result in result variable and print it on the screen using fmt.Println() function.
Example
package main
import "fmt"
// function to find sum
func findSum(mat [][]int, n int) int {
var sum int = 0
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
if i+j == 2 {
sum = sum + mat[i][j]
}
}
}
return sum
}
// calling main()
func main() {
mat := [][]int{
{1, 1, 2},
{4, 5, 6},
{8, 9, 3},
}
fmt.Println("The given matrix is: \n")
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
fmt.Print(mat[i][j], "\t")
}
fmt.Println()
}
fmt.Println()
var n int = 3
result := findSum(mat, n)
fmt.Printf("\nSum of right diagonal elements is: %d", result)
}
Output
The given matrix is: 1 1 2 4 5 6 8 9 3 Sum of right diagonal elements is: 15
Conclusion
We have successfully compiled and executed a go language program to find the sum of diagonal elements of a 3 X 3 matrix along with examples. In the first example, we found the sum of left diagonal of a matrix and in the second example, we found the sum of right diagonal of matrix respectively.
