1
1
import java .util .Scanner ;
2
2
3
- final class sparse {
4
- private sparse () {
3
+ final class Sparse {
4
+ private Sparse () {
5
5
}
6
6
7
7
static double sparsity (int [][] mat , int rows , int cols , int zero ) {
@@ -10,26 +10,28 @@ static double sparsity(int[][] mat, int rows, int cols, int zero) {
10
10
}
11
11
12
12
public static void main (String [] args ) {
13
- Scanner in = new Scanner (System .in );
14
- System .out .println ("Enter number of rows in matrix: " );
15
- int n = in .nextInt ();
16
- System .out .println ("Enter number of columns in matrix: " );
17
- int m = in .nextInt ();
13
+ Scanner in = new Scanner (System .in ); // Initialize Scanner
14
+ try {
15
+ System .out .println ("Enter number of rows in matrix: " );
16
+ int n = in .nextInt ();
17
+ System .out .println ("Enter number of columns in matrix: " );
18
+ int m = in .nextInt ();
18
19
19
- int [][] mat = new int [n ][m ];
20
- int count = 0 ; // Counter for zeros
20
+ int [][] mat = new int [n ][m ];
21
+ int count = 0 ; // Counter for zeros
21
22
22
- System .out .println ("Enter Matrix elements: " );
23
- for (int i = 0 ; i < n ; i ++) {
24
- for (int j = 0 ; j < m ; j ++) {
25
- mat [i ][j ] = in .nextInt ();
26
- if (mat [i ][j ] == 0 ) {
27
- count ++; // Increment zero count directly
23
+ System .out .println ("Enter Matrix elements: " );
24
+ for (int i = 0 ; i < n ; i ++) {
25
+ for (int j = 0 ; j < m ; j ++) {
26
+ mat [i ][j ] = in .nextInt ();
27
+ if (mat [i ][j ] == 0 ) {
28
+ count ++; // Increment zero count directly
29
+ }
28
30
}
29
31
}
30
- }
31
32
32
- double sparsityValue = sparsity (mat , n , m , count );
33
- System .out .println ("Sparsity of the matrix is: " + sparsityValue );
33
+ double sparsityValue = sparsity (mat , n , m , count );
34
+ System .out .println ("Sparsity of the matrix is: " + sparsityValue );
35
+ }
34
36
}
35
37
}
0 commit comments