-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3_2_highest_median_sales.cpp
121 lines (96 loc) · 2.53 KB
/
3_2_highest_median_sales.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
#include <iostream>
#define D_NUM_AGENTS 3
#define D_NUM_MONTHS 12
using namespace std;
/*
Highest median sales using arrays only
*/
int compareFunc(const void * val1, const void * val2);
float getMedian(int arr[], const int ARR_SIZE);
float getMedianWithQSort(int arr[], const int ARR_SIZE);
int main()
{
int sales[D_NUM_AGENTS][D_NUM_MONTHS] = {
{1856, 498, 30924, 87478, 328, 2653, 387, 3754, 387587, 2873, 276, 32},
{5865, 5456, 3983, 6464, 9957, 4785, 3875, 3838, 4959, 1122, 7766, 2534},
{23, 55, 67, 99, 265, 376, 232, 223, 4546, 564, 4544, 3434}
};
float maxMedian = 0;
float median = 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;
}
//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);
cin.get();
return 0;
}
float getMedian(int arr[], const int ARR_SIZE)
{
//Using selection sort on its half
float median = 0;
bool odd = ARR_SIZE % 2;
int centerIdx = ARR_SIZE / 2;
int startIdx = 0;
int endIdx = ARR_SIZE-1;
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;
int maxMedian = 0;
bool odd = ARR_SIZE % 2;
int centerIdx = ARR_SIZE / 2;
qsort(arr, ARR_SIZE, sizeof(int), compareFunc);
if (odd)
{
median = arr[centerIdx];
}
else
{
median = (arr[centerIdx - 1] + arr[centerIdx]) / 2.0;
}
return median;
}
int compareFunc(const void * val1, const void * val2)
{
return *(int*)(val1) - *(int*)(val2);
}