Java Program to find Matrix is a Sparse Matrix

Write a Java Program to find Matrix is a Sparse Matrix with an example. Any matrix is called a Sparse Matrix if it contains a large number of zeros. The math formula to find the Sparse Matrix is Total Zeros >= (rows * columns)/2.

We declared a 3 * 3 sp_arr integer matrix in this Sparse Matrix example. Next, we used for loop to iterate the Matrix. We used the If statement to check for zero within the for loop. Based on the result, we are adding the total. Next, we used the If Else statement to check whether the total zero’s total number is greater than (rows * columns)/2. Based on the result, Java code print the output.

public class SparseMatrix {

	public static void main(String[] args) {	
		int i, j, total = 0;
		
		int[][] sp_arr = {{0, 0, 0}, {0, 1, 0}, {5, 4, 9}, {3, 0, 0}};
		
		int rows = sp_arr.length;
		int columns = sp_arr[0].length;
			
		for(i = 0; i < rows ; i++) {
			for(j = 0; j < columns; j++){
				if(sp_arr[i][j] == 0) {
					total++;
				}
			}
		}	
		if(total > ((rows * columns)/2)) {
			System.out.println("\nMatrix is a Sparse Matrix");
		}
		else {
			System.out.println("\nMatrix is Not a Sparse Matrix");
		}
	}
}
Java Program to find Matrix is a Sparse Matrix 1

This Java Sparse Matrix example code is the same as the above. Here, we manually added the row size and column size. We also changed the array a bit and added 9 to the first row and second column.

public class SparseMatrix {

	public static void main(String[] args) {	
		int i, j, total = 0;
		
		int[][] sp_arr = {{0, 9, 0}, {0, 1, 0}, {5, 4, 9}, {3, 0, 0}};
			
		for(i = 0; i < 4 ; i++) {
			for(j = 0; j < 3; j++){
				if(sp_arr[i][j] == 0) {
					total++;
				}
			}
		}	
		if(total > ((4 * 3)/2)) {
			System.out.println("\nIt is a Sparse Matrix");
		}
		else {
			System.out.println("\nIt is Not a Sparse Matrix");
		}
	}
}
It is Not a Sparse Matrix

Java Program to find Matrix is a Sparse Matrix Example 2

This Java code is the same as the above. However, this Sparse code allows the user to enter the number of rows, columns, and matrix items.

import java.util.Scanner;

public class SparseMatrix {
	private static Scanner sc;
	
	public static void main(String[] args) {
		
		int i, j, rows, columns, total = 0;
		
		sc= new Scanner(System.in);
		
		System.out.println("\n Enter Rows and Columns :  ");
		rows = sc.nextInt();
		columns = sc.nextInt();
		
		int[][] arr = new int[rows][columns];
		
		System.out.println("\n Please Enter the Sparse Matrix Items :  ");
		for(i = 0; i < rows; i++) {
			for(j = 0; j < columns; j++) {
				arr[i][j] = sc.nextInt();
			}		
		}
		
		for(i = 0; i < rows ; i++)
		{
			for(j = 0; j < columns; j++)
			{
				if(arr[i][j] == 0) {
					total++;
				}
			}
		}
		
		if(total > ((rows * columns)/2)) {
			System.out.println("\nIt is a Sparse Matrix");
		}
		else {
			System.out.println("\nIt is Not a Sparse Matrix");
		}
	}
}

Sparse Matrix output

 Enter Rows and Columns :  
3 3

 Please Enter the Sparse Matrix Items :  
10 20 0
0 0 0
90 0 0

It is a Sparse Matrix

Let me try with less number of zeros.

 Enter Rows and Columns :  
3 3

 Please Enter the Sparse Matrix Items :  
1 2 3
0 0 0
5 0 9

It is Not a Sparse Matrix