-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayMath
More file actions
177 lines (109 loc) · 3.46 KB
/
arrayMath
File metadata and controls
177 lines (109 loc) · 3.46 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
import java.util.Arrays;
public class MartinHaileyHW07 {
public static void main(String[] args) {
// TODO Auto-generated method stub
final int TOTAL_RESPONSES = 9;
final int RATING_MIN = 0;
final int RATING_MAX = 5;
int[] results = new int[TOTAL_RESPONSES];
double arrayMean = 0;
double arrayMedian = 0;
System.out.println(TOTAL_RESPONSES +" people took the survey\r\n"
+ "The ratings were from 0 to 5\r\n");
fillArray(results, RATING_MIN, RATING_MAX);
System.out.println("******* Survey Results **********");
displayArray(results);
arrayMean = calculateMean(results);
System.out.printf("\nThe survey average is %.1f \n", arrayMean);
int[] sortedResults = new int[TOTAL_RESPONSES];
//copy results array to sorted array
for (int i = 0; i < results.length; i++) {
sortedResults[i] = results[i];
}
sortedResults = createSortedArray(results);
System.out.println("\n******* Sorted Survey Results **********");
displayArray(sortedResults);
arrayMedian = calculateMedian(sortedResults);
System.out.printf("\nThe survey median is %.1f\n", arrayMedian);
int[] frequency = new int[results.length];
fillFrequencyArray(sortedResults, frequency);
displayFrequency(frequency, RATING_MIN, RATING_MAX);
}//end of main
//Fill an array with random survey results.
public static void fillArray(int numbers[], int min, int max) {
for (int i = 0; i < numbers.length; i++) {
numbers[i] += (int)(Math.random()*(max-min + 1)+ min);
//System.out.println(number[i]);
}
}
//display array
public static void displayArray(int numbers[]) {
for (int i = 0; i < numbers.length; i++) {
System.out.printf("results[%d] = %d \n",i, numbers[i]);
}
}
//mean
public static double calculateMean(int numbers[]) {
double mean = 0;
double sum = 0;
for (int i = 0; i < numbers.length; i++) {
sum = sum + numbers[i];
}
mean = sum / numbers.length;
return mean;
}
//sort array
public static int[] createSortedArray (int [] numbers) {
int [] sortedArray = new int [numbers.length];
for (int i = 0; i < numbers.length; i++) {
sortedArray[i] = numbers[i];
}
for (int i=0 ; i < numbers.length; i++) {
Arrays.sort(sortedArray);
}
return sortedArray;
}
//median
public static double calculateMedian(int numbers[]) {
double median = 0;
if(numbers.length %2 == 0) {
median = ((numbers[(numbers.length-1)/2]) + (numbers[(numbers.length-1)/2]))/2;
//median = median /2;
}else {
median = numbers[(numbers.length-1)/2];
}
return median;
}
//frequency
public static void fillFrequencyArray(int numbers[], int freq[]) {
int visited = -1;
for(int i = 0; i <numbers.length; i++) {
int count = 1;
for(int j = i+1; j < numbers.length; j++) {
if(numbers[i] == numbers[j]) {
count++;
//this avoids counting the same thing again
freq[j] = visited;
}
}
if(freq[i] != visited) {
freq[i] = count;
}
}
//display
for(int i=0; i < freq.length; i++) {
if(freq[i] != visited) {
System.out.println("Frequency of " +numbers[i]+ " : " +freq[i]);
}
}
System.out.println("\n");
}
public static void displayFrequency(int freq[], int min, int max) {
for(int i=min; i < freq.length; i++) {
if(freq[i] != -1) {
System.out.println("Frequency of " + min + " : " +freq[i]);
min++;
}
}
}
}//end of class