-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoDimensionalArray.java
More file actions
211 lines (156 loc) · 3.43 KB
/
TwoDimensionalArray.java
File metadata and controls
211 lines (156 loc) · 3.43 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import java.util.Scanner;
public class TwoDimensionalArray {
public static int[][] takeInput2D(){
Scanner s =new Scanner(System.in);
System.out.println("Enter no. of rows");
int rows=s.nextInt();
System.out.println("Enter no. of columns");
int columns =s.nextInt();
int input[][]=new int[rows][columns];
for(int i=0;i<rows;i++)
{ for(int j=0;j<columns;j++){
input[i][j]=s.nextInt();
}
}
return input;
}
public static void print2D(int[][] arr){
int rows=arr.length;
int columns=arr[0].length;
for(int i=0;i<rows;i++){
for(int j=0;j<columns;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
public static void sum(int[][] arr){
int rows=arr.length;
int columns=arr[0].length;
for(int i=0;i<rows;i++){
int sum=0;
for(int j=0;j<columns;j++){
sum=sum+arr[i][j];
}
System.out.println("Sum for "+i+"row: "+sum);
}
}
public static void max(int[][] arr){
int max=Integer.MIN_VALUE;
int rows=arr.length;
int columns=arr[0].length;
for(int i=0;i<rows;i++){
int sum=0;
for(int j=0;j<columns;j++){
sum=sum+arr[i][j];
}
if(sum>max){
max=sum;
}}
for(int l=0;l<columns;l++){
int sum=0;
for(int k=0;k<rows;k++){
sum=sum+arr[k][l];
}
if(sum>max){
max=sum;
}
}
System.out.println("Maximum value is "+max);
}
public static void wave(int[][] arr){
int rows=arr.length;
int columns=arr[0].length;
for(int j=0;j<columns;j++){
if(j%2==0){
for(int i=0;i<rows;i++){
System.out.print(arr[i][j]);
}}
else{
for(int k=rows-1;k>=0;k--){
System.out.print(arr[k][j]);
}
}
}
System.out.println();
}
public static void spiral(int[][] arr){
int rows=arr.length;
int columns=arr[0].length;
int m=rows;
int n=columns;
int trackr=0;
int trackc=0;
int h=m;
int v=n;
int ho=m-1;
int ve=n-1;
int start1=0;
int start2=1;
int end3=0;
int end4=1;
for(int i=0;i<2*rows-1;i++){
for(int b=start1;b<h;b++){
System.out.print(arr[trackr][b]+" ");
trackc=b;
}
h=h-1;
start1++;
for(int a=start2;a<v;a++){
System.out.print(arr[a][trackc]+" ");
trackr=a;
}
v=v-1;
start2++;
for(int c=ho-1;c>=end3;c--){
System.out.print(arr[trackr][c]+" ");
trackc=c;
}
ho=ho-1;
end3++;
for(int d=ve-1;d>=end4;d--){
System.out.print(arr[d][trackc]+" ");
trackr=d;
}
ve=ve-1;
end4++;
}
}
public static void Spiral(int[][]arr){
int rows=arr.length;
int columns=arr[0].length;
int rowStart=0;
int rowEnd=rows-1;
int columnStart=0;
int columnEnd=columns-1;
while(columnStart<=columnEnd&&rowStart<=rowEnd)
{
for(int i=columnStart;i<=columnEnd;i++){
System.out.print(arr[rowStart][i]+" ");
}
rowStart++;
for(int j=rowStart;j<=rowEnd;j++){
System.out.print(arr[j][columnEnd]+" ");
}
columnEnd--;
for(int k=columnEnd;k>=columnStart;k--){
if(rowStart<=rowEnd){
System.out.print(arr[rowEnd][k]+" ");
}
}
rowEnd--;
for(int m=rowEnd;m>=rowStart;m--){
System.out.print(arr[m][columnStart]+" ");
}
columnStart++;
}
}
public static void main(String[] args) {
int[][] array=takeInput2D();
print2D(array);
sum(array);
max(array);
wave(array);
Spiral(array);
}
}