Question: #include #include #include #include #include #include / / Function to calculate the mean price for bid and ask of each product std::unordered _ map >

#include
#include
#include
#include
#include
#include
// Function to calculate the mean price for bid and ask of each product
std::unordered_map> calculate_mean_prices(const std::string& filename){
std::ifstream file(filename);
if (!file.is_open()){
std::cerr << "Error opening file: "<< filename << std::endl;
return {};
}
// Map to store accumulated prices and counts for bid and ask for each product
std::unordered_map> productPrices;
std::string line;
while (std::getline(file, line)){
std::istringstream iss(line);
std::string product, bidAsk;
double price;
// Assuming the product is in the second column, bid/ask in the third column, and price in the fifth column
if (iss >> std::quoted(product)>> std::quoted(bidAsk)>> price){
if (bidAsk == "BID" || bidAsk == "ASK"){
// Accumulate prices and counts separately for bid and ask
auto& pair = productPrices[product];
bidAsk == "BID" ? pair.first += price : pair.second += price;
}
}
}
// Calculate mean prices for bid and ask for each product
for (auto& entry : productPrices){
auto& pair = entry.second;
pair.first /= pair.second; // Calculate mean for bid
pair.second /= pair.second; // Calculate mean for ask
}
return productPrices;
}
int main(){
// Example CSV file name (replace with your actual file name)
std::string filename ="20200601.csv";
// Calculate mean prices for bid and ask
auto meanPrices = calculate_mean_prices(filename);
// Print the results
std::cout << std::left << std::setw(20)<< "Product" << std::setw(20)<< "Mean Bid Price" << "Mean Ask Price" << std::endl;
for (const auto& entry : meanPrices){
std::cout << std::setw(20)<< entry.first << std::setw(20)<< entry.second.first << entry.second.second << std::endl;
}
return 0;
}
so for some reason the output doesnt want to take the info from my csv file and idk why so it just outputs an empty table of Product Mean Bid Price Mean Ask Price
would appreciate any quick fix <3

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!