-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeanMedianMode.java
More file actions
73 lines (57 loc) · 1.82 KB
/
MeanMedianMode.java
File metadata and controls
73 lines (57 loc) · 1.82 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
//2005 - #3
import java.util.*;
public class MeanMedianMode {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
Scanner inputString = new Scanner(System.in);
int times = input.nextInt();
for(int i = 0; i < times; i++){
int size = input.nextInt();//THIS CAN EXCEED INT VALUES: PROBABLY NOT GOOD
ArrayList<Integer> nums = new ArrayList<Integer>();
int sum = 0;
for(int x = 0; x < size; x++){
int temp = input.nextLine();
sum += temp;
nums.add(temp);
}
double average = (double) temp / (double) size;
System.out.println("Mean: " + average);
//Median
Collections.sort(nums);
int index = nums.size();
if(index % 2 == 0){
//Two medians
System.out.println("Median(s): " + nums.get(index/2) + " " + nums.get((index/2) - 1));
} else {
System.out.println("Median(s): " + nums.get(index/2));
}
//Restriction is that numbers are between 0 and 99. Use the hint
int high = -1;
ArrayList<Integer> mode = new ArrayList<Integer>();
for(int x = 0; x <= 99; x++){
int counter = 0;
int test = nums.get(x);
if(test == -1) continue;
for(int j = x + 1; j <= 99; j++){
if(test == nums.get(x)){
counter++;
nums.set(j, -1);
}
}
if(counter > high){
high = counter;
mode.clear();
mode.add(test);
} else if {
mode.add(test);
}
nums.set(x, -1);
}
System.out.print("Modes(s): ");
for(int x = 0; x < mode.size(); x++){
System.out.print(mode.get(x) + " ");
}
System.out.println("(occurs " + high + " times)");
}
}
}