Skip to content

Commit d817935

Browse files
authored
Update Sparsity.java
1 parent 0674ad4 commit d817935

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed
Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import java.util.Scanner;
22

3-
final class sparse {
4-
private sparse() {
3+
final class Sparse {
4+
private Sparse() {
55
}
66

77
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) {
1010
}
1111

1212
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();
1819

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
2122

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+
}
2830
}
2931
}
30-
}
3132

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+
}
3436
}
3537
}

0 commit comments

Comments
 (0)