fork download
  1. #include <algorithm>
  2. #include <ctime>
  3. #include <iomanip>
  4. #include <iostream>
  5. #include <iterator>
  6. #include <numeric>
  7. #include <sstream>
  8. #include <vector>
  9.  
  10. using namespace std;
  11.  
  12. int main() {
  13. istringstream fin("95 100 120 130 135 145 155 185 190 160 130 120");
  14. const vector<int> vec{ istream_iterator<int>(fin), istream_iterator<int>() };
  15.  
  16. cout << "The year's average monthly rainfall was " << accumulate(cbegin(vec), cend(vec), 0.0) / vec.size() << endl;
  17.  
  18. const auto its = minmax_element(cbegin(vec), cend(vec));
  19. const tm minimum{0, 0, 0, 1, distance(cbegin(vec), its.first)};
  20. const tm maximum{0, 0, 0, 1, distance(cbegin(vec), its.second)};
  21.  
  22. cout << put_time(&minimum, "%B") << " has the highest rainfall (" << *its.first << "mm)\n" << put_time(&maximum, "%B") << " has the lowest rainfall (" << *its.second << "mm)\n";
  23.  
  24. cout << "Month Rainfall(mm) Classification\n" << left;
  25.  
  26. char month[10];
  27.  
  28. for (tm i{ 0, 0, 0, 1, 0 }; i.tm_mon < vec.size(); ++i.tm_mon) {
  29. strftime(month, 10, "%B", &i);
  30.  
  31. if (vec[i.tm_mon] <= 100) {
  32. cout << setw(9) << month << setw(15) << vec[i.tm_mon] << "Dry\n";
  33. } else if (vec[i.tm_mon] <= 159) {
  34. cout << setw(9) << month << setw(15) << vec[i.tm_mon] << "Average\n";
  35. } else {
  36. cout << setw(9) << month << setw(15) << vec[i.tm_mon] << "Rainy\n";
  37. }
  38. }
  39. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
The year's average monthly rainfall was 138.75
January has the highest rainfall (95mm)
September has the lowest rainfall (190mm)
Month    Rainfall(mm)   Classification
January  95             Dry
February 100            Dry
March    120            Average
April    130            Average
May      135            Average
June     145            Average
July     155            Average
August   185            Rainy
September190            Rainy
October  160            Rainy
November 130            Average
December 120            Average