- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang Program To Interchange Elements Of First And Last In A Matrix Across Rows
In this article, we will write a go language program to interchange elements of first and last row in a matrix.
Interchange Elements Of The First And Last Row In A Matrix Using An External Function
In this program, we will write a go language program to interchange the elements of the first and last row of a 3 X 3 matrix using an external function.
Algorithm
Step 1 − Import the fmt package.
Step 2 − Create a function to interchange the first and last row of a 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 temporarily in the t variable initialized above.
Step 4 − Now, store the elements of the last row in place of first one and store back the value stored in temporarily variable to the last row.
Step 5 − Repeat these processes until whole rows values 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 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 for i := 0; i < 3; i++ { t = mat[0][i] mat[0][i] = mat[rows-1][i] mat[rows-1][i] = 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 obtained after interchanging first and last row is: \n") 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 obtained after interchanging first and last row is: 8 9 10 4 5 6 10 1 2
Interchange The First And Last Row Of A Matrix Of Order 4 Or Above
In this program, we will see that how we can interchange the elements of top and bottom rows of a matrix of order 4 or above.
Algorithm
Step 1 − Import the fmt package.
Step 2 − Now, start the main() function. Here, initialize a 3 X 3 matrix and print it on the screen.
Step 3 − Define two variables of integer type and assign values to them. Use for loop to iterate over the matrix elements and store each element temporarily in the t variable initialized above.
Step 4 − Now, store the elements of last row in place of first one and store back the value stored in temporarily variable to the last row.
Step 5 − Repeat these processes until whole rows values are interchanged.
Step 6 − Now, print the matrix on the screen using for loops and fmt.Println() function.
Example
package main import "fmt" // calling the main() function 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() var t int = 0 var rows int = 4 for i := 0; i < 4; i++ { t = mat[0][i] mat[0][i] = mat[rows-1][i] mat[rows-1][i] = t } fmt.Println("The matrix obtained after interchanging first and last row is: \n") 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 76 4 7 6 5 3 2 1 8 9 9 7 7 The matrix obtained after interchanging first and last row is: 9 9 7 7 4 7 6 5 3 2 1 8 8 9 7 6
Conclusion
We have successfully compiled and executed a go language program to interchange the elements of first and last rows 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.