java program to check if it is a sparse matrix

Solutions on MaxInterview for java program to check if it is a sparse matrix by the best coders in the world

showing results for - "java program to check if it is a sparse matrix"
Cameron
30 Apr 2018
1import java.util;
2public class SparseMatrix
3{
4   public static void main(String[] args)
5   {
6      Scanner sc = new Scanner(System.in);
7      System.out.println("Please enter dimensions of sparse matrix: ");
8      int x = sc.nextInt();
9      int y = sc.nextInt();
10      int[][] arrNumber = new int[x][y];
11      int zeros = 0;
12      System.out.println("Please enter elements of sparse matrix: ");
13      for(int a = 0; a < x; a++)
14      {
15         for(int b = 0; b < y; b++)
16         {
17            arrNumber[a][b] = sc.nextInt();
18            if(arrNumber[a][b] == 0)
19            {
20               zeros++;
21            }
22         }
23      }	 
24      if(zeros > (x * y) / 2)
25      {
26         System.out.println("Given matrix is sparse matrix.");
27      }
28      else
29      {
30         System.out.println("Given matrix is not a sparse matrix.");
31      }	 
32      sc.close();
33   }
34}