- 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 Columns
In this article, we will write a go language program to interchange elements of the first and last column in a matrix.
Interchange Elements Of The First And Last Column 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 column 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 columns 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 column in place of the first one and store back the value stored in the temporary variable to the last column.
Step 5 − Repeat these processes till whole column 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 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 columns func matrixInter(mat [][]int) { var t int = 0 var col int = 3 for i := 0; i < 3; i++ { t = mat[i][0] mat[i][0] = mat[i][col-1] mat[i][col-1] = t } } // creating the main() function 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 column 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 column is: 2 1 10 6 5 4 10 9 8
Interchange First And Last Column Of A Matrix Of Order 4 Or Above
In this program, we will see that how we can interchange the elements of first and last columns 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 4 X 4 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 column in place of first one and store back the value stored in temporarily variable to the last column.
Step 5 − Repeat these processes until whole column 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 col int = 4 for i := 0; i < 4; i++ { t = mat[i][0] mat[i][0] = mat[i][col-1] mat[i][col-1] = t } fmt.Println("The matrix obtained after interchanging first and last column 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 7 6 4 7 6 5 3 2 1 8 9 9 7 7 The matrix obtained after interchanging first and last column is: 6 9 7 8 5 7 6 4 8 2 1 3 7 9 7 9
Conclusion
We have successfully compiled and executed a go language program to interchange the elements of the first and last columns 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.