1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
#include <iostream>
#include <string>
#include <vector>
#include <valarray>
#include <algorithm>
using namespace std;
using ROW = valarray<int>;
void print( const string &title, const vector<ROW> &M )
{
if ( title != "" ) cout << title << '\n';
for ( auto &row : M )
{
for ( auto e : row ) cout << e << '\t';
cout << "Average: " << (double)row.sum() / row.size() << '\n';
}
}
int main()
{
vector<ROW> M = { { 11, 12, 13, 14, 16 },
{ 21, 22, 55, 23, 24 },
{ 31, 32, 33, 34, 65 },
{ 13, 33, 44, 55, 55 } };
print( "Before:", M );
// Sort for DECREASING average
sort( M.begin(), M.end(), []( ROW a, ROW b ){ return a.sum() > b.sum(); } );
print( "\nAfter:", M );
}
|