Find the average exclude 2 largest and 2 smallest

int nValues[30] = {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 };

for(int count=0; count<30; count++)
{
if(nValues[count] > largest)
largest = nValues[count];
}

for(int count=0; count<30; count++)
{
if(nValues[count] > scndLargest)
{
if(nValues[count] == largest)
continue;
scndLargest = nValues[count];
}
}

This is my code so far...suppose the largest number is 182 and the other largest number is also 182...but im having 180 as the other largest number...can anyone help ?
The logic is messy to keep two largest and two smallest. It's probably easiest just to scan the array twice: the first time to find the extrema and mark their positions; the second time to scan the unmarked array positions and find the second set of extrema.

Do you mean: find the average after having excluded the extrema?

You will probably get more answers if you give complete code and put it in code tags (first item in the formatting menu).

You haven't initialised largest or scndLargest; (at least, not in the amount of code that you have shown).
Last edited on
Perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <algorithm>
#include <iostream>
#include <numeric>

int main() {
	int nValues[] {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};

	const auto nsize {std::size(nValues)};

	std::sort(nValues, nValues + nsize);

	const auto sum {std::accumulate(nValues + 2, nValues + nsize - 2, 0)};

	std::cout << "The average is " << sum / (nsize - 4.0) << '\n';
}

Last edited on
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 );
}


Outliers: 155 159 182 182
Average without outliers is 169.385 
Last edited on
Seeplus, in your call to std::accumulate, shouldn't nsize-2 be nsize-4 to skip the largest 2 values?

Edit: seeplus's code is correct. See explanation below.
Last edited on
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
#include <algorithm>
#include <iostream>
#include <numeric>
#include <iterator>

int main()
{
   int nValues[] { 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 };

   std::sort(std::begin(nValues), std::end(nValues));

   double sum { };

   for (auto itr { std::cbegin(nValues) + 2 }; itr != (std::cend(nValues) - 2); ++itr)
   {
      sum += *itr;
   }

   const auto nsize { std::size(nValues) };

   std::cout << "The total average is  " << sum / nsize << '\n';
   std::cout << "The summed average is " << sum / (nsize - 4) << '\n';
}
The total average is  146.8
The summed average is 169.385

@seeplus, your average calculation at line 19 is a bit odd, missing a couple of parentheses, shouldn't it be
sum / (nsize - 4)?
Seeplus, in your call to std::accumulate, shouldn't nsize-2 be nsize-4 to skip the largest 2 values?


 
std::accumulate(nValues + 2, nValues + nsize - 2, 0)


This will skip the first 2 values and skip the last 2 values - which as nValues is now sorted skip the 2 largest and 2 smallest.

@seeplus, your average calculation at line 19 is a bit odd, missing a couple of parentheses, shouldn't it be
sum / (nsize - 4)?


Er, yes, quite, Ok you got me. Mea Culpa. Code above changed. Used 4.0 so that the result is double.


The average is 169.385


Always test code!
Last edited on
Topic archived. No new replies allowed.