Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <unordered_map>
- #include <string>
- using namespace std;
- int priceCheck(const vector<string>& products,
- const vector<double>& productPrices,
- const vector<string>& productSold,
- const vector<double>& soldPrices) {
- // Step 1: Map product names to correct prices
- unordered_map<string, double> priceMap;
- for (size_t i = 0; i < products.size(); ++i) {
- priceMap[products[i]] = productPrices[i];
- }
- // Step 2: Compare each sold item price with correct price
- int errorCount = 0;
- for (size_t i = 0; i < productSold.size(); ++i) {
- string soldProduct = productSold[i];
- double correctPrice = priceMap[soldProduct];
- if (abs(correctPrice - soldPrices[i]) > 1e-6) {
- ++errorCount;
- }
- }
- return errorCount;
- }
- int main() {
- vector<string> products = {"eggs", "milk", "cheese"};
- vector<double> productPrices = {2.89, 3.29, 5.79};
- vector<string> productSold = {"eggs", "eggs", "cheese", "milk"};
- vector<double> soldPrices = {2.89, 2.99, 5.97, 3.29};
- int errors = priceCheck(products, productPrices, productSold, soldPrices);
- cout << "Number of pricing errors: " << errors << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment