Advertisement
maxim_shlyahtin

107

Nov 24th, 2021
1,129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <fstream>
  5. #include <algorithm>
  6.  
  7.  
  8.  
  9. int main()    
  10. {  
  11.     using namespace std;
  12.     int med, count = 0;
  13.     long long sum = 0;
  14.     double half;
  15.     vector <int> a;
  16.     vector <string> v;
  17.     string path = "nice.txt";
  18.     ifstream fin;
  19.     fin.open(path);
  20.  
  21.     if (!fin.is_open())
  22.     {
  23.         cout << "error" << endl;
  24.     }
  25.     else
  26.     {
  27.         string str;
  28.         while (!fin.eof())
  29.         {
  30.             getline(fin, str);
  31.             v.push_back(str);
  32.         }
  33.     }
  34.     fin.close();
  35.     for (int i = 0; i < (int)v.size() - 1; i++)
  36.     {  
  37.         try {
  38.             int res = stoi(v[i]);
  39.             a.push_back(res);
  40.         }
  41.         catch (std::invalid_argument e) {
  42.             cout << "caught invalid argument exception\n";
  43.             cout << i;
  44.         }
  45.     }
  46.     for (int i = 0; i < (int)a.size(); i++) {
  47.         sum += a[i];
  48.     }
  49.     sort(a.begin(), a.end());
  50.     med = a[a.size() / 2];
  51.     half = (double)sum / (double)a.size();
  52.     cout << half << " " << med << endl;
  53.     for (int i = 0; i < (int)a.size(); i++) {
  54.         if (a[i] >= half && a[i] <= med)
  55.             count++;
  56.     }
  57.     cout << count;
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement