Java Program to Check Sparse Matrix

You are currently viewing Java Program to Check Sparse Matrix

Java program to check sparse matrix

import java.util.Scanner;

public class MatrixSparse {
  public static void main(String args[]) {
    //scanner class object creation
    Scanner sc = new Scanner(System.in);

    //input numbers of rows and cols
    System.out.print("Enter the dimensions of the matrix : ");
    int m = sc.nextInt();
    int n = sc.nextInt();

    //declare two_d array (matrix) object
    double[][] mat = new double[m][n];

    //variable to store zero count
    //initializing it with 0
    int zeros = 0;

    //input matrix 
    System.out.println("Enter the elements of the matrix : ");
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++) {
        mat[i][j] = sc.nextDouble();
        if (mat[i][j] == 0) {
          //counting zeros
          zeros++;
        }
      }
    }

    //check condiion
    if (zeros > (m * n) / 2) {
      System.out.println("The matrix is a sparse matrix");
    } else {
      System.out.println("The matrix is not a sparse matrix");
    }

    sc.close();
  }
}

Outputs

Enter the dimensions of the matrix : 3 3
Enter the elements of the matrix : 
1 1 1
0 0 0 
1 1 1
The matrix is not a sparse matrix

Enter the dimensions of the matrix : 4 4
Enter the elements of the matrix : 
1 0 0 1
0 1 0 1
0 1 0 1
0 0 0 1
The matrix is a sparse matrix

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply