Golang Program To Interchange The Diagonals


In this article, we will write a go language program to interchange the diagonal elements of a matrix.

Exchange the diagonal elements of a matrix using an external function

In this program, we will write a go language program to interchange the diagonal elements of a 3 X 3 matrix using a user-defined function.

Algorithm

Step 1 − Import the fmt package.

Step 2 − Create a function to interchange the diagonal elements of the matrix. In this function, we have defined two variables of integer type and assigned values to them.

Step 3 − Use for loop to iterate over the matrix elements and store each element of the left diagonal temporarily in the t variable initialized above.

Step 4 − Now, store the elements of the right diagonal in place of the left one and store back the value stored in the temporary variable to the right diagonal.

Step 5 − Repeat these processes till whole diagonal elements are interchanged.

Step 6 − Now, start the main() function. Here, initialized a 3 X 3 matrix and print it on the screen.

Step 7 − Call the matrixInter() function by passing the matrix as an argument to the function.

Step 8 − Now, print the matrix on the screen using for loops and fmt.Println() function.

Example

package main
import "fmt"

// function to interchange rows
func matrixInter(mat [][]int) {
   var t int = 0
   var rows int = 3
   var i int
   for i = 0; i < 3; i++ {
      t = mat[i][i]
      mat[i][i] = mat[i][rows-i-1]
      mat[i][rows-i-1] = t
   }
}
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()
   matrixInter(mat)
   fmt.Println("The Matrix Items after Interchanging Diagonals is:")
   for i := 0; i < 3; i++ {
      for j := 0; j < 3; j++ {
         fmt.Print(mat[i][j], "\t")
      }
      fmt.Println()
   }
}

Output

The given matrix is: 

10	1	2	
4	5	6	
8	9	10	

The Matrix Items after Interchanging Diagonals is:
2	1	10	
4	5	6	
10	9	8

Interchange The Diagonal Elements Of A Matrix Of Order 4 Or Above

In this program, we will write a go language program to interchange the elements of a matrix of order 4 or above using the main() function.

Algorithm

Step 1 − Import the fmt package.

Step 2 − Create a function to interchange the diagonal elements of the matrix. In this function, we have defined two variables of integer type and assigned values to them.

Step 3 − Use for loop to iterate over the matrix elements and store each element of the left diagonal temporarily in the t variable initialized above.

Step 4 − Now, store the elements of the right diagonal in place of the left one and store back the value stored in the temporarily variable to the right diagonal.

Step 5 − Repeat these processes until whole diagonal elements are interchanged.

Step 6 − Now, start the main() function. Here, initialized a 4 X 4 matrix or above and print it on the screen.

Step 7 − Call the matrixInter() function by passing the matrix as an argument to the function.

Step 8 − Now, print the matrix on the screen using for loops and fmt.Println() function.

Example

package main
import "fmt"

// function to interchange diagonals
func matrixInter(mat [][]int) {
   var temp int = 0
   var rows int = 4
   var i int
   for i = range mat {
      temp = mat[i][i]
      mat[i][i] = mat[i][rows-i-1]
      mat[i][rows-i-1] = temp
   }
}
func main() {
   mat := [][]int{
      {8, 9, 7, 6},
      {4, 7, 6, 5},
      {3, 2, 1, 8},
      {9, 9, 7, 7},
   }
   fmt.Println("The given matrix is: \n")
   for i := 0; i < 4; i++ {
      for j := 0; j < 4; j++ {
         fmt.Print(mat[i][j], "\t")
      }
      fmt.Println()
   }
   fmt.Println()
   matrixInter(mat)
   fmt.Println("The Matrix Items after Interchanging Diagonals is:")
   for i := 0; i < 4; i++ {
      for j := 0; j < 4; j++ {
         fmt.Print(mat[i][j], "\t")
      }
      fmt.Println()
   }
}

Output

The given matrix is: 

8	9	7	6	
4	7	6	5	
3	2	1	8	
9	9	7	7	

The Matrix Items after Interchanging Diagonals is:
6	9	7	8	
4	6	7	5	
3	1	2	8	
7	9	7	9	

Conclusion

We have successfully compiled and executed a go language program to interchange the diagonal elements of a matrix along with examples. In the first example, we have used the logic to interchange elements of a 3 X 3 matrix while in the second one we are extending the logic to matrices of the order 4 or above.

Updated on: 02-Jan-2023

73 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements