asdfg0998

Untitled

May 4th, 2025
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <unordered_map>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. int priceCheck(const vector<string>& products,
  9.                const vector<double>& productPrices,
  10.                const vector<string>& productSold,
  11.                const vector<double>& soldPrices) {
  12.    
  13.     // Step 1: Map product names to correct prices
  14.     unordered_map<string, double> priceMap;
  15.     for (size_t i = 0; i < products.size(); ++i) {
  16.         priceMap[products[i]] = productPrices[i];
  17.     }
  18.  
  19.     // Step 2: Compare each sold item price with correct price
  20.     int errorCount = 0;
  21.     for (size_t i = 0; i < productSold.size(); ++i) {
  22.         string soldProduct = productSold[i];
  23.         double correctPrice = priceMap[soldProduct];
  24.         if (abs(correctPrice - soldPrices[i]) > 1e-6) {
  25.             ++errorCount;
  26.         }
  27.     }
  28.  
  29.     return errorCount;
  30. }
  31. int main() {
  32.     vector<string> products = {"eggs", "milk", "cheese"};
  33.     vector<double> productPrices = {2.89, 3.29, 5.79};
  34.     vector<string> productSold = {"eggs", "eggs", "cheese", "milk"};
  35.     vector<double> soldPrices = {2.89, 2.99, 5.97, 3.29};
  36.  
  37.     int errors = priceCheck(products, productPrices, productSold, soldPrices);
  38.     cout << "Number of pricing errors: " << errors << endl;
  39.  
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment