Golang Program To Find The Transpose


In this article, we will write a go language program to find the transpose of a matrix. A matrix is a collection of numbers that are arranged in rows and columns, which is a two-dimensional array.

Find The Transpose Of A Matrix

The following code illustrates an example to find the transpose of a matrix.

Algorithm

Step 1 − Import the fmt package.

Step 2 − Call the main() function.

Step 3 − Initialize a 2-d array called matrixA and matrix and store elements in it.

Step 4 − Print the matrices on the screen.

Step 5 − To find the transpose of these matrices iterate over the matrix using for loop and make a new matrix from the old one by changing each row of the matrix to the column.

Step 6 − Once we have iterated over the matrix. Print the new matrix thus formed on the screen.

Step 7 − Repeat the process for the second matrix and print it on the screen.

Example

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

Output

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

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

Find The Transpose Of A Matrix Using The Range Function

Let us now look at another method to find the transpose of a matrix in Go programming language.

Algorithm

Step 1 − Import the fmt package.

Step 2 − Call the main() function.

Step 3 − Initialize a 2-d array called matrixA and store elements in it. print this matrix on the screen using fmt.Println() function.

Step 4 − To find the transpose of this matrix iterate over the matrix using the range function and make a new matrix from the old one by changing each row of the matrix to a column.

Step 5 − Once we have iterated over the matrix. Print the new matrix thus formed on the screen.

Example

package main
import (
   "fmt"
)
func main() {
   var i, j int
   var transposeMat [3][3]int
   matrixA := [3][3]int{
      {0, 1, 2},
      {4, 5, 6},
      {8, 9, 10},
   }
   fmt.Println("The first matrix is:")
   for i = 0; i < 3; i++ {
      for j = 0; j < 3; j++ {
         fmt.Print(matrixA[i][j], "\t")
      }
      fmt.Println()
   }
   fmt.Println()
   for i, rows := range matrixA {
      for j := range rows {
         transposeMat[j][i] = matrixA[i][j]
      }
   }
   fmt.Println("The transpose of matrix, matrixA is:")
   for i, rows := range matrixA {
      for j := range rows {
         fmt.Print(transposeMat[i][j], "\t")
      }
      fmt.Println()
   }
   fmt.Println()
}

Output

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

The transpose of matrix, matrixA is:
0	4	8	
1	5	9	
2	6	10	

Conclusion

We have successfully compiled and executed a golang program to find the transpose of a matrix along with examples.

Updated on: 02-Jan-2023

372 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements