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>
using namespace std;
template <typename T> double avsum( int &counter, T a )
{
counter = 1;
return a;
}
template <typename T, typename... Tail> double avsum( int &counter, T a, Tail... b )
{
double total = a + avsum( counter, b... );
counter++;
return total;
}
template <typename T> double average( T a )
{
return a;
}
template <typename T, typename... Tail> double average( T a, Tail... b )
{
int counter;
return avsum( counter, a, b... ) / counter;
}
int main()
{
cout << average( 10 ) << '\n';
cout << average( 8.5, 16.5 ) << '\n';
cout << average( 10.5, 21.1, 4 ) << '\n';
cout << average( 1, 2, 3, 4, 5 ) << '\n';
}
|