Retail sales index Ireland (2005-2016)

#include <iostream> #include <vector> using namespace std; float avg(vector<float> vals) { double s = 0; for(const auto val : vals) { s += val; } return s / vals.size(); } int main() { int year_months = 12, start_year = 2005; // via Central Statistics Office (http://www.cso.ie) vector<float> index_vals = {104.2, 103.0, 99.4, 99.2, 98.7, 98.3, 98.4, 97.6, 94.9, 93.6, 95.3, 115.4, 116.2, 112.3, 107.8, 108.2, 105.6, 105.9, 106.8, 104.4, 100.0, 101.7, 103.3, 123.6, 125.5, 119.4, 116.5, 117.9, 115.4, 114.1, 116.4, 109.7, 107.1, 107.9, 109.0, 131.6, 131.6, 121.8, 114.4, 115.8, 109.3, 106.1, 113.2, 100.8, 100.8, 97.9, 98.4, 119.5, 95.5, 93.4, 90.4, 90.2, 89.0, 90.7, 91.5, 87.5, 86.2, 83.9, 85.4, 105.3, 88.3, 91.8, 90.9, 90.3, 88.3, 88.2, 88.5, 86.1, 84.3, 83.9, 84.5, 101.4, 91.2, 90.7, 88.9, 88.9, 87.3, 88.7, 87.7, 83.4, 81.5, 81.3, 84.9, 105.0, 90.1, 88.4, 88.8, 88.0, 86.3, 83.8, 86.6, 83.7, 83.4, 84.3, 85.2, 104.5, 88.8, 88.5, 87.1, 85.4, 85.0, 83.0, 90.4, 84.4, 83.5, 82.5, 85.5, 106.2, 95.9, 91.1, 91.5, 89.5, 87.5, 85.6, 96.4, 88.4, 86.7, 84.8, 87.1, 109.0, 101.3, 96.2, 98.4, 95.9, 91.7, 86.9, 103.4, 93.0, 90.7, 87.7, 91.9, 111.8, 112.0, 104.8, 100.1, 99.1, 96.4, 89.2, 109.6, 95.3, 92.2, 89.3, 93.6, 112.0}, v; // Which year had the highest average index value? size_t years = index_vals.size() / year_months; for(size_t i = 0; i < years; ++i) { v = {}; for(size_t j = year_months * i; j < (i+1) * year_months; ++j) { v.push_back(index_vals[j]); } cout << "Year " << start_year + i << ": " << avg(v) << endl; } /* Year 2005: 99.8333 Year 2006: 107.983 Year 2007: 115.875 Year 2008: 110.8 Year 2009: 90.75 Year 2010: 88.875 Year 2011: 88.2917 Year 2012: 87.7583 Year 2013: 87.525 Year 2014: 91.125 Year 2015: 95.7417 Year 2016: 99.4667 The answer is year 2007. Since 2013, the index seems to be increasing again. */ // In which year, August had the highest retail sales index value? string month_name = "August"; int month_idx = 7; int mod12 = 0; for(size_t i = 0; i < index_vals.size(); ++i) { mod12 = i % 12; if(mod12 == month_idx) { cout << month_name << "," << start_year << ": " << index_vals[i] << endl; } if(mod12 == 0 && i > 0) {start_year++;} } /* August,2005: 97.6 August,2006: 104.4 August,2007: 109.7 August,2008: 100.8 August,2009: 87.5 August,2010: 86.1 August,2011: 83.4 August,2012: 83.7 August,2013: 84.4 August,2014: 88.4 August,2015: 93 August,2016: 95.3 Once again - it's year 2007. But Augusts are increasing since 2011. */ return 0; }