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 37 38
|
#include <iostream>
#include <algorithm>
using namespace std;
template<typename T>
void checkLarge( T test, T &second, T &first )
{
if ( test < second ) return;
second = min( test, first );
first = max( test, first );
}
template<typename T>
void checkSmall( T test, T &second, T &first )
{
if ( test > second ) return;
second = max( test, first );
first = min( test, first );
}
int main()
{
int A[] = { 182, 163, 174, 155, 168, 160, 159, 168, 172, 180,
173, 171, 177, 175, 169, 161, 165, 159, 169, 172,
174, 175, 166, 172, 168, 163, 182, 180, 165, 165 };
int N = sizeof A / sizeof A[0];
int smallest = min( A[0], A[1] ), secondSmallest = max( A[0], A[1] );
int largest = secondSmallest, secondLargest = smallest;
int sum = A[0] + A[1];
for ( int i = 2; i < N; i++ )
{
sum += A[i];
checkSmall( A[i], secondSmallest, smallest );
checkLarge( A[i], secondLargest , largest );
}
cout << "Outliers: " << smallest << " " << secondSmallest << " " << secondLargest << " " << largest << '\n';
cout << "Average without outliers is " << ( sum - smallest - secondSmallest - secondLargest - largest ) / ( N - 4.0 );
}
| |