-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetMatricesZero.java
More file actions
65 lines (55 loc) · 1.71 KB
/
Copy pathsetMatricesZero.java
File metadata and controls
65 lines (55 loc) · 1.71 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
//Question: Given m x n integer matrix. if an element is zero, set its entire row and column to zero.
package twoDArray;
import java.util.Scanner;
public class setMatricesZero {
public static void main(String[] args) {
System.out.print("ENTER THE NUMBER OF ROWS AND NUMBER OF COLUMNS: ");
Scanner sc=new Scanner(System.in);
int m=sc.nextInt();
int n=sc.nextInt();
int[][] arr=new int[m][n];
System.out.println("ENTER THE ELEMENTS:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
arr[i][j]=sc.nextInt();
}
}
int[] row=new int[m];
int[] col=new int[n];
for(int i=0; i<m; i++){
row[i]=arr[i][0];
}
for(int j=0; j<n; j++){
col[j]=arr[0][j];
}
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
if(arr[i][j]==0){
row[i]=0;
col[j]=0;
}
}
}
for(int i=0; i<m; i++){
if(row[i]==0){
for(int j=0; j<n; j++){
arr[i][j]=0;
}
}
}
for(int j=0; j<n; j++){
if(col[j]==0){
for(int i=0; i<m; i++){
arr[i][j]=0;
}
}
}
System.out.println("FINAL ARRAY IS:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(arr[i][j]);
}
System.out.println();
}
}
}