-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatistics.java
More file actions
124 lines (113 loc) · 3.64 KB
/
Copy pathStatistics.java
File metadata and controls
124 lines (113 loc) · 3.64 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
/*
Poorvi Mathur
1/29/23
Description of program:
Returns average, median, mode, and most of the array inputted by the user.
*/
import java.util.Arrays;
import java.util.Scanner;
public class Statistics
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Welcome to Statistics!\n" +
"This program will calculate the average, median, \n" +
"mode, and most of any number of (double) values!\n" +
"\n" +
"How many values will you be entering?\n");
int n = input.nextInt();
double[] arr = new double[n];
for (int i = 1; i < n + 1; i++)
{
System.out.println(" > Please enter value #" + i + ":");
arr[i - 1] = input.nextDouble();
}
double average = average(arr);
System.out.println("Average:" + average);
double median = median(arr);
System.out.println("Median:" + median);
double[] mode = mode(arr);
System.out.println("Mode:");
ArrayHelper.display(mode); // displays the actual array
double[] most = most(arr);
System.out.println("Most:");
ArrayHelper.display(most);
}
public static double average(double[] values)
{
double sum = 0;
for(double x : values)
{
sum += x;
}
sum = sum / values.length; // divides the sum by array length that is inputted by user.
return sum;
}
public static double median(double[] values)
{
values = sort(values);
int mid = values.length / 2;
if (values.length % 2 == 0)
return (values[mid - 1] + values[mid]) / 2.0; //gets average of middle two numbers
return values[mid];
}
public static double[] mode(double[] values)
{
double[] count = new double[values.length];
for(int i = 0; i < values.length; i++)
{
count[i] = ArrayHelper.count(values, values[i]); //calling count method to count how many times a number was in each array.
}
double max = ArrayHelper.max(count);
double[] most = new double[values.length];
int countMost = 0;
for (int i = 0; i < values.length; i++)
{
double counter = count[i];
double values2 = values[i];
boolean inc = true;
if (counter == max)
{
for (int j = 0; j < countMost; j++)
{
if (most[j] == values2)
{
inc = false;
}
}
if(inc)
most[countMost++] = values2;
}
}
return count;
}
public static double[] most(double[] values)
{
int count = 0;
for (int i = 0; i < values.length; i++)
{
if (values[i] >= Statistics.average(values)) //compares with average
count++;
}
double[] values2 = new double[count];
int counter = 0;
for (int i = 0; i < values.length; i++)
{
if (values[i] >= Statistics.average(values))
{
values2[counter] = values[i];
counter++;
}
}
return values2;
}
// returns a sorted copy of values
// does not modify vaules
public static double[] sort(double[] values)
{
double[] temp = values.clone();
Arrays.sort(temp);
return temp;
}
}