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.

Updated on: 10-Jan-2023

124 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements