-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZeroMatrix.java
More file actions
98 lines (73 loc) · 1.79 KB
/
ZeroMatrix.java
File metadata and controls
98 lines (73 loc) · 1.79 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.sai;
import java.util.Scanner;
public class ZeroMatrix {
int[][] matrix;
int row;
int column;
public void create() {
System.out.println("Matrix Creation");
@SuppressWarnings("resource")
Scanner scan = new Scanner(System.in);
System.out.println("Enter the number of rows");
row = scan.nextInt();
System.out.println("Enter the number of columns");
column = scan.nextInt();
System.out.println("Enter the elements ");
matrix = new int[row][column];
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
matrix[i][j] = scan.nextInt();
}
}
}
private void setZeros(int[][] matrix2) {
boolean[] row = new boolean[matrix.length];
boolean[] column = new boolean[matrix[0].length];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if(matrix[i][j]==0){
row[i] = true;
column[j] = true;
}
}
}
for (int i = 0; i < row.length; i++) {
if (row[i]) {
nullifyRow(matrix, i);
}
}
for (int j = 0; j < column.length; j++) {
if (column[j]) {
nullifyColumn(matrix, j);
}
}
}
void nullifyRow(int[][] matrix2, int i) {
for (int j = 0; j < matrix[0].length; j++) {
matrix[i][j] = 0;
}
}
void nullifyColumn(int[][] matrix2, int j) {
for (int i = 0; i < matrix.length; i++) {
matrix[i][j] = 0;
}
}
void display() {
System.out.println("\nThe Matrix is :");
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
System.out.print(" " + matrix[i][j]);
}
System.out.println();
}
}
public static void main(String[] args) {
ZeroMatrix zm = new ZeroMatrix();
zm.create();
System.out.println("Before manipulating");
zm.display();
zm.setZeros(zm.matrix);
System.out.println("After manipulating");
zm.display();
}
}