Signup/Sign In

Java Program To Determine If a Given Matrix is a Sparse Matrix

In this tutorial, we will learn how to determine if a given matrix is a sparse matrix. A matrix is said to be a sparse matrix if most of the elements of that matrix are 0. But before moving forward, if you are not familiar with the concepts of the array, then do check the article Arrays in Java.

Below is the pictorial representation for the same.

Input: Enter the Matrix Elements:

1 4 0

0 0 0

4 0 0

Output: It is a sparse matrix.

Program 1: To Determine If the Given Matrix is a Sparse Matrix

In this program, we will learn how to determine if a given matrix is a sparse matrix when the values are user-defined. Here, we will ask the user to input the values and then, we will check whether the given matrix is a sparse matrix or not.

Algorithm

  1. Start
  2. Declare variables to store the size of the matrix.
  3. Ask the user to initialize the number of rows and columns.
  4. Declare a matrix.
  5. Ask the user to initialize the elements of the matrix.
  6. Print the original matrix
  7. Declare a variable to store the size of the matrix.
  8. Declare a variable to count the number of 0 elements in the matrix.
  9. Use a loop to count all the zero elements.
  10. Increment the count if any 0 elements are found.
  11. Check if the count is greater than half of the size.
  12. If it is greater, then print it as a sparse matrix.
  13. Else print it is not a sparse matrix.
  14. Stop.

Below is the code for the same.

//Java Program to check whether the given matrix is sparse or not*/
import java.util.Scanner; 
public class Main 
{ 
    public static void main(String[] args) 
    { 
        // declare variables 
        int m, n;  
        // To take input from the user
        Scanner sc = new Scanner(System.in); 
        System.out.println("Enter the number of rows ");   
        // Initialize the number of rows 
        m = sc.nextInt();   
        System.out.println("Enter the number of columns ");  
        // Initialize the number of columns 
        n = sc.nextInt();   
        // declare a mxn order array 
        int a[][] = new int[m][n];   
        System.out.println("Enter all the values of matrix "); 
        // Initialize the matrix elements
        for (int i = 0; i < m; i++) 
        { 
            for (int j = 0; j < n; j++) 
            { 
                a[i][j] = sc.nextInt();                 
            } 
        }    
        System.out.println("Original Matrix:"); 
        // print the original matrix 
        for (int i = 0; i < m; i++) 
        { 
            for (int j = 0; j < n; j++) 
            { 
                    System.out.print(a[i][j] + " "); 
            } 
            System.out.println(""); 
        } 
        int size= m*n;   //Stores the size of the matrix 
        int count=0;    //Variable to check for the number of 0 elements        
        //Loop to count all zero element present in matrix    
        for(int i = 0; i < m; i++)
        {    
            for(int j = 0; j < n; j++)
            {    
                if(a[i][j] == 0)    //Check if element is 0 or not
                    count++;    //Increment the count if 0 element is found
            }    
        }        
        if(count>(size/2))
        System.out.println("It is a sparse matrix");
        else
        System.out.println("It is not a sparse matrix");           
    } 
}


Enter the number of rows 3
Enter the number of columns 3
Enter all the values of matrix 1 2 0 0 0 0 0 0 0
Original Matrix:
1 2 0
0 0 0
0 0 0
It is a sparse matrix

Program 2: To Determine If the Given Matrix is a Sparse Matrix

In this program, we will learn how to determine if a given matrix is a sparse matrix when the values are pre-defined. Here, the elements for the matrix are pre-defined in the program. So, based on the values of that matrix we will check whether the given matrix is a sparse matrix or not.

Algorithm

  1. Start
  2. Declare and initialize a matrix.
  3. Declare variables to store the number of rows and columns of a matrix.
  4. Print the original matrix.
  5. Declare a variable to store the size of the matrix.
  6. Declare a variable to count the number of 0 elements in the matrix.
  7. Use a loop to count all the zero elements.
  8. Increment the count if any 0 elements are found.
  9. Check if the count is greater than half of the size.
  10. If it is greater, then print it as a sparse matrix.
  11. Else print it is not a sparse matrix.
  12. Stop.

Below is the code for the same.

//Java Program to check whether the given matrix is sparse or not*/
public class Main 
{ 
    public static void main(String[] args) 
    {         
        // declare and initialize a matrix 
        int a[][] = {{ 2, 9, 8 }, { 7, 6, 4 }, { 3, 9, 2 } };   
        int m=a.length;   //Stores the number of rows in a matrix
        int n=a[0].length;   //Stores the number of columns in a matrix 
         // print the original matrix 
        System.out.println("Original Matrix:"); 
        for (int i = 0; i < m; i++) 
        { 
            for (int j = 0; j < n; j++) 
            { 
                    System.out.print(a[i][j] + " "); 
            } 
            System.out.println(""); 
        }   
        int size= m*n;   //Stores the size of the matrix        
        int count=0;    //Variable to check for the number of 0 elements        
        //Loop to count all zero element present in matrix    
        for(int i = 0; i < m; i++)
        {    
            for(int j = 0; j < n; j++)
            {    
                if(a[i][j] == 0)    //Check if element is 0 or not
                    count++;    //Increment the count if 0 element is found            }    
        }        
        if(count>(size/2))
        System.out.println("It is a sparse matrix");
        else
        System.out.println("It is not a sparse matrix");           
    } 
}


Original Matrix:
2 9 8
7 6 4
3 9 2
It is not a sparse matrix



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.