-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoDArray.java
More file actions
293 lines (267 loc) · 6.88 KB
/
TwoDArray.java
File metadata and controls
293 lines (267 loc) · 6.88 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import javax.swing.*;
public class TwoDArray
{
private static String input;
private static String[] b;
private static int[][] array, c, d;
static int low, high, key, cell;
private static int minimum = Integer.MAX_VALUE, maximum = Integer.MIN_VALUE, sum = 0;
private static double average = 0.0, standardDeviation0 = 0.0, standardDeviation1 = 0.0;
private static TwoDArray originalArray;
public TwoDArray(int r, int c, int l, int h)
{
array = new int[r][c];
for (int i=0; i<r; i++)
{
for (int j=0; j<c; j++)
{
array[i][j] = l + (int)((h-l+1)*Math.random());
}
}
}
public TwoDArray()
{
low = 1;
high = 100;
array = new int[10][10];
for (int i=0; i<10; i++)
{
for (int j=0; j<10; j++)
{
array[i][j] = low + (int)((high-low+1)*Math.random());
}
}
}
public int[][] getArray() {return array;}
public static int getLow() {return low;}
public static int getHigh() {return high;}
public void createArray()
{
int r = promptInt("Please enter number of rows (1-50):", "# Rows of Two-Dimensional Array", 1, 50);
int c = promptInt("Please enter number of columns (1-50):", "# Columns of Two-Dimensional Array", 1, 50);
array = new int[r][c];
low = promptInt("Please enter the lowest value (<= highest):", "Lowest Value in the Array", -100000, 100000);
high = promptInt("Please enter the highest value (greater than lowest, up to 1000):", "Highest Value in the Array", Math.max(low + 1, -99999), 1000);
for (int i=0; i<array.length; i++)
for (int j=0; j<array[i].length; j++)
array[i][j] = low + (int)((high-low+1)*Math.random());
}
public static int[][] initializeArray(int[][] a, String command)
{
if ("Create New Array".equals(command) || "Create Array".equals(command))
{
originalArray = new TwoDArray();
originalArray.createArray();
a = originalArray.getArray();
}
else if (a != null && ( "Array Minimum".equals(command) || "Array Maximum".equals(command)
|| "Array Average".equals(command) || "Array Standard Deviation".equals(command)
|| "Array Search".equals(command)))
{
}
else if (a == null && ( "Array Minimum".equals(command) || "Array Maximum".equals(command)
|| "Array Average".equals(command) || "Array Standard Deviation".equals(command)
|| "Array Search".equals(command)))
{
originalArray = new TwoDArray();
originalArray.createArray();
a = originalArray.getArray();
}
else
{
originalArray = new TwoDArray();
originalArray.createArray();
a = originalArray.getArray();
}
return a;
}
public static int getMin(int[][] a)
{
minimum = Integer.MAX_VALUE;
for (int row = 0; row < a.length; row++)
{
for (int col = 0; col < a[row].length; col++)
{
if (a[row][col] < minimum)
{
minimum = a[row][col];
}
}
}
return minimum;
}
public static int getMax(int[][] a)
{
maximum = Integer.MIN_VALUE;
for (int row = 0; row < a.length; row++)
{
for (int col = 0; col < a[row].length; col++)
{
if (a[row][col] > maximum)
{
maximum = a[row][col];
}
}
}
return maximum;
}
public static double getAvg(int[][] a)
{
sum = 0;
for (int row = 0; row < a.length; row++)
{
for (int col = 0; col < a[row].length; col++)
{
sum += a[row][col];
}
}
average = sum/(double)(a.length * a[0].length);
return roundDigits(average, 3);
}
public static double getSD(int[][] a)
{
standardDeviation0 = 0.0;
standardDeviation1 = 0.0;
double avg = getAvg(a);
for (int row = 0; row < a.length; row++)
{
for (int col = 0; col < a[row].length; col++)
{
standardDeviation0 += Math.pow(a[row][col] - avg, 2);
}
}
standardDeviation1 = Math.pow((standardDeviation0/(a.length * a[0].length)), 0.5);
return roundDigits(standardDeviation1, 3);
}
public static String[] searchArray(int[][] a)
{
while (true)
{
input = JOptionPane.showInputDialog(null, "Please enter search key.", "Enter Search Key", JOptionPane.QUESTION_MESSAGE);
if (input == null)
{
b = new String[]{"false", "<cancelled>", "", ""};
return b;
}
try
{
key = Integer.parseInt(input.trim());
break;
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "Please enter a valid integer.", "Invalid Input", JOptionPane.WARNING_MESSAGE);
}
}
b = new String[4];
b[0] = "false";
b[1] = Integer.toString(key);
for (int row = 0; row < a.length; row++)
{
for (int col = 0; col < a[row].length; col++)
{
if (a[row][col] == key)
{
b[0] = "true";
b[2] = Integer.toString(row);
b[3] = Integer.toString(col);
}
}
}
return b;
}
public static int[][] addArray(int[][] a, int[][] b)
{
c = new int[a.length][a[0].length];
for (int row = 0; row < a.length; row++)
{
for (int col = 0; col < a[row].length; col++)
{
c[row][col] = a[row][col] + b[row][col];
}
}
return c;
}
public static int[][] subtractArray(int[][] a, int[][] b)
{
c = new int[a.length][a[0].length];
for (int row = 0; row < a.length; row++)
{
for (int col = 0; col < a[row].length; col++)
{
c[row][col] = a[row][col] - b[row][col];
}
}
return c;
}
public static int[][] multiplyArray(int[][] a, int[][] b)
{
// Validate matrix dimensions
if (a == null || b == null) return null;
if (a[0].length != b.length) throw new IllegalArgumentException("Incompatible matrix sizes for multiplication");
d = new int[a.length][b[0].length];
for (int row = 0; row < a.length; row++)
{
for (int col = 0; col < b[0].length; col++)
{
d[row][col] = multiplyArrayCell(a, b, row, col);
}
}
return d;
}
public static int multiplyArrayCell(int[][] a, int[][] b, int row, int col)
{
cell = 0;
for (int i = 0; i < b.length; i++)
{
cell += a[row][i] * b[i][col];
}
return cell;
}
public static int getNumberOfDigits(int n)
{
int p = 0;
if (n == 1000)
{
p = 4;
}
else if (n >= 100 && n < 1000)
{
p = 3;
}
else if (n >= 10 && n < 100)
{
p = 2;
}
return p;
}
public static double roundDigits(double x, int d)
{
return (Math.round(x*Math.pow(10, d))/Math.pow(10,d));
}
private static int promptInt(String message, String title, int min, int max)
{
while (true)
{
String s = JOptionPane.showInputDialog(null, message, title, JOptionPane.QUESTION_MESSAGE);
if (s == null) {
JOptionPane.showMessageDialog(null, "Input cancelled. Please provide a value.", "Input Required", JOptionPane.WARNING_MESSAGE);
continue;
}
try
{
int v = Integer.parseInt(s.trim());
if (v < min || v > max)
{
JOptionPane.showMessageDialog(null, "Please enter a value between " + min + " and " + max + ".", "Out of Range", JOptionPane.WARNING_MESSAGE);
continue;
}
return v;
}
catch (NumberFormatException ex)
{
JOptionPane.showMessageDialog(null, "Please enter a valid integer.", "Invalid Input", JOptionPane.WARNING_MESSAGE);
}
}
}
}