-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTMax.java
More file actions
67 lines (55 loc) · 1.61 KB
/
TMax.java
File metadata and controls
67 lines (55 loc) · 1.61 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
// Name = Rahul Navin
// Student id = 261062889
import java.util.Arrays;
import java.util.Random;
public class TMax {
public static void main(String[] args) {
int seed = Integer.parseInt(args[0]);
double[] array = getRandomArray(seed);
System.out.println(thirdBiggest(array));
}
public static double[] getRandomArray(long seed){
int lowerBound = 2;
int upperBound = 10;
// TODO change only the following part.
Random ran = new Random(seed);
int size = ran.nextInt((upperBound-lowerBound)+ 1) + lowerBound;
Random r = new Random(seed);
double [] arr = new double[size];
for (int i=0; i<arr.length; i++)
{
arr[i] = r.nextDouble();
}
System.out.println(Arrays.toString(arr));
return arr;
}
public static double thirdBiggest(double[] array){
// TODO change only the following part.
System.out.println(" ");
double d;
int total = array.length;
if (total> 2)
{
for (int i = 0; i < total; i++)
{
for (int j = i + 1; j < total; j++)
{
if (array[i] > array[j])
{
d = array[i];
array[i] = array[j];
array[j] = d;
}
}
}
return array[total-3];
}
else
{
if (array[0]> array[1])
return array[0];
else
return array[1];
}
}
}