-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatistics.java
More file actions
102 lines (97 loc) · 3.79 KB
/
Copy pathStatistics.java
File metadata and controls
102 lines (97 loc) · 3.79 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
import java.util.Arrays;
import java.util.Scanner;
public class Statistics {
public static void main(String[] args) {
System.out.println("Hello, Welcome to Statistics\nBased on the double values you " +
"enter, you will recieve:\nThe Mean, The Mode, The Median, The " +
"Most\nLet's get started");
Scanner input = new Scanner(System.in);
System.out.println("\nHow many numbers will your data set have?");
int size = input.nextInt();
double[] statisticsInfo = new double[size];
for (int i = 0; i < statisticsInfo.length; i++) {
System.out.println("Enter your " + (i + 1) + " double value");
Scanner numberInput = new Scanner(System.in);
double number = numberInput.nextDouble();
statisticsInfo[i] = number;
}
for (int i = 0; i < statisticsInfo.length; i++) {
System.out.println("The " + (i + 1) + " number is " + statisticsInfo[i]);
}
System.out.println();
System.out.println("The average is " + average(statisticsInfo));
System.out.println("The median is " + median(statisticsInfo));
System.out.println("The mode is ");
ArrayHelper.display(mode(statisticsInfo));
System.out.println("The most is ");
ArrayHelper.display(most(statisticsInfo));
}
public static double average(double[] values) {
double sum = 0;
double mean;
for (int i = 0; i < values.length; i++) {
sum = sum + values[i];
}
mean = sum / values.length;
return mean;
}
public static double median(double[] values) {
values = sort(values);
if (values.length % 2 == 1) {
int valuesIndex = (int) values.length / 2;
return values[valuesIndex];
} else {
int valuesIndexLower = (int) values.length / 2;
int valuesIndexHigher = ((int) values.length / 2) + 1;
return ((values[valuesIndexLower] + values[valuesIndexHigher]) / 2);
}
}
public static double[] mode(double[] values) {
int modeIndex = 0;
double[] mode = {};
int arrayAdder = 0;
double[] savedMode = {};
for (int i = 0; i < values.length; i++) {
int currentModeIndex = 0;
for (int j = 0; j < values.length - 1; j++) {
if (values[i] == values[j + 1]) {
currentModeIndex++;
}
}
if (currentModeIndex > modeIndex) {
modeIndex = currentModeIndex;
arrayAdder++;
}
if (currentModeIndex == modeIndex) {
mode = new double[1];
mode[0] = values[i];
if (!(ArrayHelper.contains(savedMode, mode[0]))) {
savedMode = ArrayHelper.merge(savedMode, mode);
}
}
}
return savedMode;
}
public static double[] most(double[] values) {
double[] combined = {0};
double[] savedCombined = {};
double average = average(values);
for (int i = 0; i < values.length; i++) {
if (values[i] >= average) {
combined = new double[1];
combined[0] = values[i];
if (!(ArrayHelper.contains(savedCombined, combined[0]))) {
savedCombined = ArrayHelper.merge(combined, savedCombined);
}
}
}
return savedCombined;
}
// returns a sorted copy of values
// does not modify values
public static double[] sort(double[] values) {
double[] temp = values.clone();
Arrays.sort(temp);
return temp;
}
}