Repository navigation
Expand file tree
/
Copy pathStatistics.cpp
More file actions
303 lines (253 loc) · 9.1 KB
/
Copy pathStatistics.cpp
File metadata and controls
303 lines (253 loc) · 9.1 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include <map>
#include <iostream>
#include <cmath>
#include <unordered_map>
#include <vector>
#include <string>
#include "Statistics.h"
#include "Car.h"
#include "Sorting.h"
// Function to sort cars by region and count manual and automatic transmissions for each region
void sortAndCountByRegionAndTransmission(vector<Car> &cars)
{
mergeSortString(cars, "STATE");
vector<TransmissionCounts> regionTransmissionCounts;
for (const Car &car : cars)
{
bool found = false;
for (const auto &counts : regionTransmissionCounts)
{
if (counts.state == car.state)
{
found = true;
break;
}
}
if (!found)
{
regionTransmissionCounts.push_back({car.state, 0, 0});
}
}
for (const Car &car : cars)
{
for (auto &counts : regionTransmissionCounts)
{
if (counts.state == car.state)
{
if (car.transmission == "manual")
{
counts.manualCount++;
}
else if (car.transmission == "automatic")
{
counts.automaticCount++;
}
break;
}
}
}
for (auto &counts : regionTransmissionCounts)
{
int totalTransmissions = counts.manualCount + counts.automaticCount;
counts.manualCount = round((counts.manualCount * 100.0) / totalTransmissions);
counts.automaticCount = 100 - counts.manualCount;
}
cout << "Region\tMT(%)\tAT(%)\n";
for (const auto &counts : regionTransmissionCounts)
{
cout << counts.state << "\t" << counts.manualCount << "%\t" << counts.automaticCount << "%\n";
}
}
// Function to count the best-selling model by make
void countBestSellingModelByMake(vector<Car> &cars) {
vector<Car> sortedCars = mergeSortString(cars, "MAKE");
string currentMake = "";
int currentIndex = 0;
while (currentIndex < sortedCars.size()) {
currentMake = sortedCars[currentIndex].make;
int endIndex = currentIndex;
while (endIndex < sortedCars.size() && sortedCars[endIndex].make == currentMake) {
endIndex++;
}
vector<Car> makeCars(sortedCars.begin() + currentIndex, sortedCars.begin() + endIndex);
vector<string> modelList;
vector<int> modelCounts;
string bestSellingModel = "";
int maxCount = 0;
// Find unique models and count occurrences
for (const Car &makeCar : makeCars) {
bool found = false;
for (size_t i = 0; i < modelList.size(); ++i) {
if (modelList[i] == makeCar.model) {
modelCounts[i]++;
found = true;
break;
}
}
if (!found) {
modelList.push_back(makeCar.model);
modelCounts.push_back(1);
}
}
// Find the best selling model
for (size_t i = 0; i < modelList.size(); ++i) {
if (modelCounts[i] > maxCount) {
maxCount = modelCounts[i];
bestSellingModel = modelList[i];
}
}
cout << currentMake << ": " << bestSellingModel << endl;
currentIndex = endIndex;
}
}
// Function to calculate the average selling price by transmission type
pair<float, float> calculateAveragePriceByTransmission(const vector<Car> &cars) {
vector<Car> automaticCars;
vector<Car> manualCars;
for (const Car &car : cars){
if (car.transmission == "automatic"){
automaticCars.push_back(car);
} else if (car.transmission == "manual"){
manualCars.push_back(car);
}
}
float totalAutomaticPrice = 0.0;
for (const Car &car : automaticCars)
{
totalAutomaticPrice += car.sellingPrice;
}
float averageAutomaticPrice = automaticCars.empty() ? 0.0 : totalAutomaticPrice / automaticCars.size();
float totalManualPrice = 0.0;
for (const Car &car : manualCars)
{
totalManualPrice += car.sellingPrice;
}
float averageManualPrice = manualCars.empty() ? 0.0 : totalManualPrice / manualCars.size();
return make_pair(averageAutomaticPrice, averageManualPrice);
}
// Function to calculate the proportions of top cars by make
unordered_map<string, pair<int, float>> calculateProportions(const vector<Car> &topCars, int topCount, const unordered_map<string, int> &totalCounts)
{
unordered_map<string, pair<int, float>> proportions;
for (const auto &car : topCars)
{
proportions[car.make].first++;
}
for (auto &pair : proportions)
{
pair.second.second = static_cast<float>(pair.second.first) / totalCounts.at(pair.first);
}
return proportions;
}
// Function to custom sort proportions in descending order
void customSortProportions(vector<pair<string, pair<int, float>>> &proportions)
{
for (int i = 0; i < proportions.size(); ++i)
{
for (int j = i + 1; j < proportions.size(); ++j)
{
if (proportions[i].second.second < proportions[j].second.second)
{
swap(proportions[i], proportions[j]);
}
}
}
}
// Function to calculate the proportions of top cars
void calculateTopCarsProportions(const vector<Car> &cars)
{
int topCount = 118080;
vector<Car> sortedCars = cars;
mergeSortInteger(sortedCars, "CONDITION_VALUE", 0, sortedCars.size() - 1);
vector<Car> topCars(sortedCars.begin(), sortedCars.begin() + min(topCount, static_cast<int>(sortedCars.size())));
unordered_map<string, int> totalCounts;
for (const auto &car : cars)
{
totalCounts[car.make]++;
}
unordered_map<string, pair<int, float>> proportions = calculateProportions(topCars, topCount, totalCounts);
vector<pair<string, pair<int, float>>> proportionsVec(proportions.begin(), proportions.end());
customSortProportions(proportionsVec);
cout << "\nProportions of each make in the top " << topCount << " cars (in descending order):\n";
for (const auto &pair : proportionsVec)
{
cout << pair.first << ": " << pair.second.first << "/" << totalCounts[pair.first]
<< " = " << pair.second.second << endl;
}
}
// Function to recognize the colors of top cars
void Color_recognizer(const vector<Car> &cars)
{
vector<Car> sortedCars = cars;
mergeSortInteger(sortedCars, "SELLINGPRICE", 0, sortedCars.size() - 1);
int numCarsToConsider = ceil(sortedCars.size() * 0.25);
vector<Car> topCars(sortedCars.begin(), sortedCars.begin() + numCarsToConsider);
vector<string> colors = {"beige", "black", "blue", "brown", "burgundy", "charcoal", "gold", "gray", "green", "lime", "off-white", "orange", "pink", "purple", "red", "silver", "turquoise", "white", "yellow"};
vector<int> counts(colors.size(), 0);
for (const Car &car : topCars)
{
for (size_t i = 0; i < colors.size(); i++)
{
if (car.color == colors[i])
{
counts[i]++;
break;
}
}
}
cout << "Color count for top 25% of cars based on selling price:\n";
for (size_t i = 0; i < colors.size(); i++)
{
cout << colors[i] << ": " << counts[i] << '\n';
}
}
// Function to recognize the interiors of top cars
void Interior_recognizer(const vector<Car> &cars)
{
vector<Car> sortedCars = cars;
mergeSortInteger(sortedCars, "SELLINGPRICE", 0, sortedCars.size() - 1);
int numCarsToConsider = ceil(sortedCars.size() * 0.25);
vector<Car> topCars(sortedCars.begin(), sortedCars.begin() + numCarsToConsider);
vector<string> interiors = {"beige", "black", "blue", "brown", "burgundy", "gold", "gray", "green", "off-white", "orange", "purple", "red", "silver", "tan", "white", "yellow"};
vector<int> counts(interiors.size(), 0);
for (const Car &car : topCars)
{
for (size_t i = 0; i < interiors.size(); i++)
{
if (car.interior == interiors[i])
{
counts[i]++;
break;
}
}
}
cout << "Interior count for top 25% of cars based on selling price:\n";
for (size_t i = 0; i < interiors.size(); i++)
{
cout << interiors[i] << ": " << counts[i] << '\n';
}
}
vector<YearData> calculateAverageByYear(const vector<Car>& cars) {
// Create a vector of YearData to store data for each year
vector<YearData> averageByYear;
// Calculate total selling price and count for each year
for (const Car& car : cars) {
bool found = false;
for (YearData& data : averageByYear) {
if (data.year == car.year) {
data.averageSellingPrice += car.sellingPrice;
data.carCount++;
found = true;
break;
}
}
if (!found) {
averageByYear.push_back({car.year, car.sellingPrice, 1});
}
}
// Calculate average selling price for each year
for (YearData& data : averageByYear) {
data.averageSellingPrice /= data.carCount;
}
return averageByYear;
}