Golang Program To Rotate Matrix Elements


In this article, we will write a go language program to rotate given matrix element.

Rotate A Matrix Using An External Function

The following code illustrates rotating a matrix anticlockwise by 90 degrees any number of times.

Algorithm

Step 1 − Import the fmt package.

Step 2 − Create a function to rotate the array elements. This function takes the array to be rotated as argument.

Step 3 − Also pass the number of times the array is to be shifted as argument. Further, initialize an empty matrix to hold the final result.

Step 4 − In the function use two for loops to intend over the matrix and update the new matrix elements at a different row each time. return the final matrix thus obtained.

Step 5 − Start the main() function. Here, create a 3 X 3 matrix and print it on the screen.

Step 6 − Call the rotateMatrix() function by passing respective parameters and store the result obtained in a separate variable.

Step 7 − Print the final result on the screen using for loops and fmt.Println() function.

Example

package main
import "fmt"

// function to rotate matrix
func rotateMatrix(X [][]int, n int) [3][3]int {
   var temp [3][3]int
   for i := 0; i < n; i++ {
      for j := 0; j < n; j++ {
         temp[n-j-1][i] = X[i][j]
      }
   }
   return temp
}
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 = 3
   result := rotateMatrix(mat, n)
   fmt.Println("The matrix obtained after rotating the above matrix to", n, "Positions is:")
   fmt.Println()
   for i := 0; i < 3; i++ {
      for j := 0; j < 3; j++ {
         fmt.Print(result[i][j], "\t")
      }
      fmt.Println()
   }
}

Output

The given matrix is: 

10	1	2	
4	5	6	
8	9	10	

The matrix obtained after rotating the above matrix to 3 Positions is:

2	6	10	
1	5	9	
10	4	8

Rotate Elements Of The Array In Clockwise Direction N Times

In this example, we will write a go language program to shift elements of the array to a clockwise direction any number of times.

Algorithm

Step 1 − Import the fmt package.

Step 2 − Start the main() function. Here, create a 3 X 3 matrix and print it on the screen.

Step 3 − Print the final result on the screen using for loops and fmt.Println() function.

Step 4 − Use for loops to intend over the array and shift the positions of the array to a clockwise direction respectively.

Step 5 − Now, print the array on the screen using fmt.Println() function.

Example

package main
import "fmt"

// calling main()
func main() {

   // initializing a 3 X 3 matrix
   mat := [3][3]int{
      {1, 3, 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()
   }
   var N int = 3
   fmt.Println()
   fmt.Println("The matrix obtained after rotating the above matrix to", N, "Positions clock wise is:")
   fmt.Println()
   for i := N - 1; i >= 0; i-- {
      for j := N - 1; j >= 0; j-- {
         fmt.Print(mat[i][j], "\t")
      }
      fmt.Println()
   }
}

Output

The given matrix is: 

1	3	2	
4	5	6	
8	9	10	

The matrix obtained after rotating the above matrix to 3 Positions clock wise is:

10	9	8	
6	5	4	
2	3	1

Rotate Elements Of An Array By 180 Degrees

In this example, we will write a go program to rotate elements of a matrix by 180 degrees

Algorithm

Step 1 − Import the fmt package.

Step 2 − Create functions naming reverseColumns(), transpose() and printMatrix() to reverse columns of the matrix, find its transpose and print the elements respectively.

Step 3 − Also pass the matrix as an argument to the function.

Step 4 − In these functions, we are using for loops to carry out the respective operation.

Step 5 − Start the main() function. Here, create a 3 X 3 matrix and print it on the screen.

Step 6 − Call the rotate180() and printMatrix() function by passing the array as argument.

Example

package main
import "fmt"
func reverseColumns(arr [][]int) {
   var c int = 3
   for i := 0; i < 3; i++ {
      for j, k := 0, c-1; j < k; j, k = j+1, k-1 {
         t := arr[j][i]
         arr[j][i] = arr[k][i]
         arr[k][i] = t
      }
   }
}
func transpose(arr [][]int) {
   for i := 0; i < 3; i++ {
      for j := i; j < 3; j++ {
         t := arr[i][j]
         arr[i][j] = arr[j][i]
         arr[j][i] = t
      }
   }
}
func printMatrix(arr [][]int) {
   for i := 0; i < 3; i++ {
      for j := 0; j < 3; j++ {
         fmt.Print(arr[i][j], "\t")
      }
      fmt.Println()
   }
}
func rotate180(arr [][]int) {
   transpose(arr)
   reverseColumns(arr)
   transpose(arr)
   reverseColumns(arr)
}

// calling main()
func main() {

   // initializing a 3 X 3 matrix
   mat := [][]int{
      {1, 3, 2},
      {4, 5, 6},
      {8, 9, 10},
   }
   
   // printing matrix
   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()
   fmt.Println("The matrix obtained after rotating the above matrix to", 3, "Positions clock wise is:")
   fmt.Println()
   rotate180(mat)
   printMatrix(mat)
}

Output

The given matrix is: 

1	3	2	
4	5	6	
8	9	10	

The matrix obtained after rotating the above matrix to 3 Positions clock wise is:

10	9	8	
6	5	4	
2	3	1

Conclusion

In this article, we have successfully executed a golang code to rotate array elements along with examples. We have seen three different examples here, in the first one; we rotated the matrix clockwise direction in 90 degreess. In the second example, we rotated the matrix N number of times and in the third example, we rotated the matrix 180 degrees.

Updated on: 02-Jan-2023

447 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements