
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print Boundary Elements of a Matrix in Golang
What is Boundary Elements?
In go programming language. the boundary elements of a matrix are the elements that are located on the outer edge of the matrix. They are located at four positions i.e the first row, the last row, the first column and at the last column. In a matrix of size m x n, the top boundary elements are the elements in the range matrix[0][0] to matrix[0][n-1], the bottom boundary elements are the elements in the range matrix[m-1][0] to matrix[m-1][n-1], the left boundary elements are the elements in the range matrix[0][0] to matrix[m-1][0], and the right boundary elements are the elements in the range matrix[0][n-1] to matrix[m-1][n-1].
Algorithm
Step 1 ? First, we need to import the fmt package.
Step 2 ? Then, create a function named printBoundary() that accepts the matrix as argument. Store the rows and columns of the matrix thus obtained in different variables.
Step 3 ? Now, iterate over each row and column of the matrix one by one and print the respective row and column as the boundary element.
Step 4 ? Here, note that to avoid printing of corner elements more than once start the next loop from rows - 2 position.
Step 5 ? Once each row and column is iterated print the corresponding element. Now, start the main() function. Here initialize a matrix and assign values to it.
Step 6 ? Then, Print the corresponding matrix on the screen by using for loops and fmt.Println() function. Once the matrix is printed call the printBoundary() function by passing the matrix as argument to it.
Step 7 ? In this manner the boundary elelemts of the matrix are printed. Now, use some more matrices and print their boundary elements too on the screen.
Example
In this example we will write a go language program to print the boundary elements of a matrix using an external function. We will print the boundary elements in the clockwise direction and will use for loops along with if conditionals in order to achieve the result.
package main import "fmt" func printBoundary(matrix [][]int) { rows := len(matrix) cols := len(matrix[0]) // Print the first row for i := 0; i < cols; i++ { fmt.Print(matrix[0][i], " ") } // Print the last column for i := 1; i < rows; i++ { fmt.Print(matrix[i][cols-1], " ") } // Print the last row for i := cols - 2; i >= 0; i-- { fmt.Print(matrix[rows-1][i], " ") } // Print the first column for i := rows - 2; i > 0; i-- { fmt.Print(matrix[i][0], " ") } } func main() { matrix1 := [][]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} var rows int = len(matrix1) var cols int = len(matrix1[0]) // printing matrix fmt.Println("The given matrix is:") for i := 0; i < rows; i++ { for j := 0; j < cols; j++ { fmt.Print(matrix1[i][j], "\t") } fmt.Println() } fmt.Println("Boundary elements of above matrix is:") printBoundary(matrix1) fmt.Println() fmt.Println() matrix2 := [][]int{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}} rows = len(matrix2) cols = len(matrix2[0]) // printing matrix fmt.Println("The second matrix is:") for i := 0; i < rows; i++ { for j := 0; j < cols; j++ { fmt.Print(matrix2[i][j], "\t") } fmt.Println() } fmt.Println("Boundary elements of above matrix is:") printBoundary(matrix2) fmt.Println() fmt.Println() matrix3 := [][]int{{1, 2, 3}, {4, 5, 6}} rows = len(matrix3) cols = len(matrix3[0]) // printing matrix fmt.Println("The given matrix is:") for i := 0; i < rows; i++ { for j := 0; j < cols; j++ { fmt.Print(matrix3[i][j], "\t") } fmt.Println() } fmt.Println("Boundary elements of above matrix is:") printBoundary(matrix3) fmt.Println() fmt.Println() }
Output
The given matrix is: 1 2 3 4 5 6 7 8 9 Boundary elements of above matrix is: 1 2 3 6 9 8 7 4 The second matrix is: 1 2 3 4 5 6 7 8 9 10 11 12 Boundary elements of above matrix is: 1 2 3 4 8 12 11 10 9 5 The given matrix is: 1 2 3 4 5 6 Boundary elements of above matrix is: 1 2 3 6 5 4
Conclusion
We have successfully compiled and executed a go language program to get the boundary elements of a matrix using an external function. The function uses different for loops to iterate over each row and column of the matrix that is received by the function as an argument and prints each row and column as the boundary elements respectively.