Advertisement
Sclafus

Min/max/average from file

Dec 1st, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. #include <sstream>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     ifstream input{"input_8.2.txt"};
  11.     vector<int> valori_tot = {};
  12.     for (string line; getline(input, line);){
  13.         istringstream sstr{line};
  14.         for (string misure; getline(sstr, misure, ',');){
  15.             int misura = stoi(misure);
  16.             valori_tot.push_back(misura);
  17.         }
  18.     }
  19.  
  20.  
  21. int sum = 0;
  22. for (int elemento : valori_tot){
  23.     sum += elemento;
  24. }
  25. float media = 0.0;
  26. media = float(sum) / valori_tot.size();
  27. cout <<"Media: "<< media << endl;
  28.  
  29. int max_value = *max_element(valori_tot.begin(), valori_tot.end());
  30. int min_value = *min_element(valori_tot.begin(), valori_tot.end());
  31. cout << "Valore massimo: " << max_value << endl;
  32. cout << "Valore minimo: " << min_value << endl;
  33.  
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement