-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3_9_median_mean_sales_2.cpp
180 lines (146 loc) · 3.68 KB
/
3_9_median_mean_sales_2.cpp
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
178
179
#include <iostream>
#define D_NUM_AGENTS 3
#define D_NUM_MONTHS 12
using namespace std;
/*
Check if array is sorted or not
*/
int compareFunc(const void * val1, const void * val2);
float getMedian(int arr[], const int ARR_SIZE);
float getMedianWithQSort(int arr[], const int ARR_SIZE);
float getMean(int arr[], const int ARR_SIZE);
int main()
{
int sales[D_NUM_AGENTS][D_NUM_MONTHS] = {
{-1, -1, 30924, 87478, 328, 2653, 387, 3754, 387587, 2873, 276, 32},
{5865, 5456, 3983, 6464, 9957, 4785, 3875, 3838, 4959, -1, -1, -1},
{-1, 55, 67, 99, 265, 376, 232, 223, 4546, 564, 4544, -1}
};
float median = 0;
float maxMedian = 0;
float mean = 0;
float maxMean = 0;
//Using selection sort on its half
for (int agentIdx = 0; agentIdx < D_NUM_AGENTS; agentIdx++)
{
//median = getMedian(sales[agentIdx], D_NUM_MONTHS);
median = getMedianWithQSort(sales[agentIdx], D_NUM_MONTHS);
if (maxMedian < median)
{
maxMedian = median;
}
mean = getMean(sales[agentIdx], D_NUM_MONTHS);
if (maxMean < mean)
{
maxMean = mean;
}
}
//Printing the sorted lists
for (int i = 0; i < D_NUM_AGENTS; i++)
{
printf("\nAgent %d: ", i+1);
for (int j = 0; j < D_NUM_MONTHS; j++)
{
cout << sales[i][j] << " ";
}
}
printf("\nMax median sales is %.1f", maxMedian);
printf("\nMax mean sales is %.1f", maxMean);
cin.get();
return 0;
}
int compareFunc(const void * val1, const void * val2)
{
return *(int*)(val1) - *(int*)(val2);
}
float getMedian(int arr[], const int ARR_SIZE)
{
// Using selection sort on its half
float median = 0;
// Find start index
int startIdx = 0;
while (arr[startIdx] == -1 && startIdx < ARR_SIZE)
{
startIdx++;
}
// Find end index
int endIdx = ARR_SIZE-1;
while (arr[endIdx] == -1 && endIdx > 0)
{
endIdx--;
}
int listSize = endIdx-startIdx+1;
bool odd = listSize % 2;
int centerIdx = listSize / 2 + startIdx;
int minimum = 0;
int minimumIdx = 0;
for (int i = startIdx; i <= centerIdx; i++)
{
minimum = arr[endIdx];
minimumIdx = endIdx;
for (int j = endIdx; j >= i; j--)
{
if (arr[j] <= minimum)
{
minimum = arr[j];
minimumIdx = j;
}
}
int temp = arr[i];
arr[minimumIdx] = temp;
arr[i] = minimum;
}
if (odd)
{
median = arr[centerIdx];
}
else
{
median = (arr[centerIdx - 1] + arr[centerIdx]) / 2.0;
}
return median;
}
float getMedianWithQSort(int arr[], const int ARR_SIZE)
{
float median = 0;
// Find start slice
int startSlice = 0;
while (arr[startSlice] == -1 && startSlice < ARR_SIZE)
{
startSlice++;
}
// Find end index
int endSlice = ARR_SIZE-1;
while (arr[endSlice] == -1 && endSlice > 0)
{
endSlice--;
}
endSlice = ARR_SIZE - endSlice - 1;
int listSize = ARR_SIZE - endSlice - startSlice;
bool odd = listSize % 2;
int centerIdx = listSize / 2 + startSlice;
//Split array
int * p_temp = &arr[startSlice];
qsort(p_temp, listSize, sizeof(int), compareFunc);
if (odd)
{
median = arr[centerIdx];
}
else
{
median = (arr[centerIdx - 1] + arr[centerIdx]) / 2.0;
}
return median;
}
float getMean(int arr[], const int ARR_SIZE)
{
int n = 0;
float sum = 0;
for(int i = 0; i < ARR_SIZE; i++)
{
if (arr[i] == -1) continue;
n++;
sum += arr[i];
}
return sum / n;
}