- 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 Determine If a Given Matrix is a Sparse Matrix
In this tutorial, we will write a go language program to determine whether a given matrix is sparce or not. A square matrix is called a sparse matrix if the number of zeroes present in the matrix are more than the non-zero elements in the matrix.
Golang Program to Check if a Matrix is Sparce
In this example, we will write a Golang program to check the sparce matrix. We will use for loops along with if conditions to achieve the result in the main section of the program.
Algorithm
Step 1 − First, we need to import the fmt package.
Step 2 − Then start the main() function. In the main() initialize the variables.
Step 3 − Now, initialize a matrix and store values to it. Further, print this matrix on the screen.
Step 4 − Store the number of rows and columns of the matrix in rows and cols variable using the len() function
Step 5 − Use a for loop to iterate over each element of the matrix and check if the current element is zero or not. If the element is zero then increment the count variable.
Step 6 − Now, if the value of count turns out to be more than half the elements in the matrix then print that the matrix is sparce otherwise print matrix is not sparce.
Step 7 − Repeat the process by initializing one more matrix.
Example
package main import "fmt" func main() { // initializing variables var i, j int var count int = 0 matrixA := [3][3]int{ {0, 1, 2}, {4, 0, 0}, {0, 0, 0}, } var rows int = len(matrixA) var cols int = len(matrixA[0]) // printing matrix fmt.Println("The first matrix is:") for i = 0; i < rows; i++ { for j = 0; j < cols; j++ { fmt.Print(matrixA[i][j], "\t") } fmt.Println() } // checking if matrix is a sparce matrix for i = 0; i < rows; i++ { for j = 0; j < cols; j++ { if matrixA[i][j] == 0 { count++ } } } if count > (rows*cols)/2 { fmt.Println("The above matrix is a sparce matrix") } else { fmt.Println("The given matrix is not sparce") } fmt.Println() // initializing another matrix matrixB := [3][3]int{ {0, 11, 12}, {13, 0, 15}, {16, 0, 18}, } count = 0 rows = len(matrixB) cols = len(matrixB[0]) fmt.Println("The second matrix is:") for i = 0; i < rows; i++ { for j = 0; j < cols; j++ { fmt.Print(matrixB[i][j], "\t") } fmt.Println() } fmt.Println() for i = 0; i < rows; i++ { for j = 0; j < cols; j++ { if matrixB[i][j] == 0 { count++ } } } if count > (rows*cols)/2 { fmt.Println("The above matrix is a sparce matrix") } else { fmt.Println("The above matrix is not sparce") } }
Output
The first matrix is: 0 1 2 4 0 0 0 0 0 The above matrix is a sparce matrix The second matrix is: 0 11 12 13 0 15 16 0 18 The above matrix is not sparce
Conclusion
We have successfully compiled and executed a go language program to check whether a given matrix is sparce or not along with examples.
- Related Articles
- Java Program To Determine If a Given Matrix is a Sparse Matrix
- JavaScript program to check if a given matrix is sparse or not
- C++ Program to Check if it is a Sparse Matrix
- Check if a given matrix is sparse or not in C++
- C++ Program to Implement Sparse Matrix
- C Program for sparse matrix
- How to convert a sparse matrix into a matrix in R?
- Swift program to check if a given square matrix is an Identity Matrix
- How to create a sparse matrix in R?
- How to create a sparse Matrix in Python?
- C++ Program to find out if a palindromic matrix can be made from a given matrix
- Golang Program To Find the Trace and Normal of a given Matrix
- Program to convert given Matrix to a Diagonal Matrix in C++
- Sparse Matrix Multiplication in C++
- Program to check if a matrix is Binary matrix or not in C++
