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 35 36
|
#include <iostream>
#include <iomanip>
#include <string>
#include <valarray>
using namespace std;
const int WIDTH = 8;
#define SW << setw( WIDTH ) <<
template <typename T> ostream &operator << ( ostream &strm, const valarray<T> &V )
{
for ( T e : V ) strm SW e << " ";
return strm;
}
int main()
{
valarray<double> A = { 4.2, 4.1, 6.3, 6.2, 5.5, 4.6, 4.7, 5.0, 6.3, 5.8, 4.6, 5.5 };
int row = 3, col = A.size() / row;
cout << fixed << setprecision( 1 );
for ( int n = 0; n < A.size(); n += col )
{
valarray<double> ROW = A[slice(n,col,1)];
cout << ROW << " | " SW ROW.sum() << '\n';
}
cout << string( col * ( WIDTH + 1), '-' ) << '\n';
for ( int c = 0; c < col; c++ )
{
valarray<double> COL = A[slice(c,row,col)];
cout SW COL.sum() << " ";
}
cout << '\n';
}
|