C++ arrays question!

I have made my code to cout the average of the values without the k smallest and k highest. I have a question. Where it says "B' SKELOS" i want to make my code so it cout the average if it replaces the k highest and the k smallest values with their near values. Can you understand the question?

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <algorithm>
#include <cmath>
#include <iomanip>

using namespace std;


bool fthinousa(float n1, float n2)
{                                
    return n1>n2;               
}

bool au3ousa(float n1, float n2)
{                              
    return n1<n2;             
}



int main()
{
    int n, k;

    cin >> n >> k;

    float b[n], result1 = 0.f, result2 = 0.f;

    float array2[(n-k)];
    float array3[(n-(2*k))];

    for (int i=0; i<n; i++)
    {
        cin >> b[i];
    }
        sort(b, b + n, au3ousa);

    for(int i=0; i<n-k; i++)
    {
        array2[i]=b[i+k];
    }
        sort(array2, array2 + (n-k), fthinousa);

    for(int i=0; i<n-k; i++)
    {
        array3[i]=array2[i+k];
    }

    for(int a = 0; a < (n-(2*k)); a++)
    {
        result1 += array3[a];
    }



    cout << setprecision(2) << fixed <<(result1 / (n-(k*2))) << endl << endl;


//
//B' SKELOS
//



    return 0;
}
Understand: no.
What are near values?

You say that you now compute average of a set from which outliers have been removed. Do you want to compute average for whole set, but only after modifying some elements?
the near=for ex. 1 is near to 2, to near to 3.... do you now understand?
Do you mean, should k=2, that array
1, 2, 3, 4, 5
is replaced with
2, 3, 3, 3, 4
?

However, sum of elements and thus average remains the same. (+k-k==0)
k=2

array={1,2,3,4,5,6,7,8,9,10}

arrayafter={3,3,3,4,5,6,7,10,10,10}
std::sort( array, array+n );
auto sum = std::accumulate( array+k, array+n-k, 0 );
sum += k * (array[k] + array[n-k-1]);
auto avg = static_cast<double>( sum ) / n;
Topic archived. No new replies allowed.