For a better understanding of this example, we always recommend you to learn the basic topics of Golang programming listed below:
A symmetric matrix is a square matrix that is equal to its transpose. Formally, Because equal matrices have equal dimensions, only square matrices can be symmetric.
for example
matrix =
2 3 6
3 4 5
6 5 9
transpose =
2 3 6
3 4 5
6 5 9
Here we are showing how to check the symmetric matrix in the Go language. Here variable mat,TMat is used to hold the matrix elements and iteration elements i,j. Read the number of rows and columns for reading matrix elements into the variables row, col. After reading, elements find the transpose of a matrix using two nested for loops
. Compare each element of the transpose matrix is equal to the original matrix. If equal it is a symmetric matrix. Finally, print the result. Given below are the steps which are used in the Go program.
STEP 1: Import the package fmt
STEP 2: Start function main()
STEP 3: Declare the matrix variables mat,TMat, and iteration variables i,j
STEP 4: Read the number of rows and columns into row, col
STEP 5: Read the matrix for finding the transpose
STEP 6: Find the transpose of a matrix using two nested for loops
STEP 7: Compare each element of the transpose matrix with the original matrix
STEP 8: If equal print the message as a symmetric matrix using fmt.print()
package main
import "fmt"
func main() {
var i, j, row, col int
var mat [10][10]int
var TMat [10][10]int
fmt.Print("Enter the number of rows and columns = ")
fmt.Scan(&row;, &col;)
fmt.Println("Enter matrix items = ")
for i = 0; i < row; i++ {
for j = 0; j < col; j++ {
fmt.Scan(&mat;[i][j])
}
}
for i = 0; i < row; i++ {
for j = 0; j < col; j++ {
TMat[j][i] = mat[i][j]
}
}
count := 1
for i = 0; i < col; i++ {
for j = 0; j < row; j++ {
if mat[i][j] != TMat[i][j] {
count++
break
}
}
}
if count == 1 {
fmt.Println("This matrix is a Symmetric Matrix")
} else {
fmt.Println("The matrix is not a Symmetric Matrix")
}
}
Enter the number of rows and columns = 2 2 Enter matrix items = 2 3 3 2 This matrix is a Symmetric Matrix