Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#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

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions