-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSparse_Matrix.java
More file actions
47 lines (40 loc) · 1.29 KB
/
Sparse_Matrix.java
File metadata and controls
47 lines (40 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.*;
public class Sparse_Matrix {
public static void main(String[] args) {
int rows, cols, size, count = 0;
int m, n, c, d;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of matrix :");
m = scan.nextInt();
n = scan.nextInt();
int matrix[][] = new int[m][n];
System.out.println("Enter the elements of matrix :");
for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
{
matrix[c][d] = scan.nextInt();
}
}
//Calculates number of rows and columns present in given matrix
rows = matrix.length;
cols = matrix[0].length;
//Calculates the size of array
size = rows * cols;
//Count all zero element present in matrix
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
if(matrix[i][j] == 0)
count++;
}
}
if(count > (size/2))
{
System.out.println("Given matrix is a sparse matrix");
}
else
{
System.out.println("Given matrix is not a sparse matrix");
}
}
}