Calculation of Median [Where is the Problem]?

Hey guys,

i'm sitting the last 2 days on this code and i just can't find the problem.
I want to calculate the median.

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
 #include <iostream>
#include <vector>
#include <algorithm>

using std::cin;
using std::cout;
using std::endl;

using namespace std;

int main() {

    vector<double> vTemps{}; // Temperaturwerte
    double temp{ 0.0};

    cout << "Geben sie Temperaturwerte ein: " << endl;
    
    while (cin >> temp ) // beliebig viele einlesen

        vTemps.push_back(temp); // speichern

    // arithmetisches Mittel:
    double sum{ 0.0};

    for (unsigned int i{0U}; i < vTemps.size(); ++i)
        sum += vTemps.at(i);

    cout << vTemps.size();

    cout << "Durchschnittliche Temperatur: " << sum / vTemps.size() << std::endl;

    // sortieren mit Funktion aus StdLib, Header algorithm
    sort(vTemps.begin(), vTemps.end());

    // M edian (nicht ganz korrekt):

    if ((vTemps.size() % 2) == 0)
        cout << "Temperatur-Median: " << (vTemps.at((vTemps.size() / 2) - 1) + vTemps.at((vTemps.size() / 2))) / 2 << endl;

    else
        cout << "Temperatur-Median-else: " << (vTemps.at((vTemps.size() / 2) ) );
                                                    

    return 0; 
What you do does not seem wrong. What is the error that you see?

Here is the same program with couple style changes and one sanity guard:
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
#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    using std::cin;
    using std::cout;

    std::vector<double> vTemps;
    double temp {0.0};
    cout << "Geben sie Temperaturwerte ein: \n";
    while ( cin >> temp )
        vTemps.push_back( temp );

    if ( vTemps.empty() ) return 1; // 0 samples

    double sum {0.0};
    for ( size_t i{0U}; i < vTemps.size(); ++i)
        sum += vTemps.at(i);
    // alternatively: http://www.cplusplus.com/reference/numeric/accumulate/

    cout << vTemps.size() << " samples\n";
    cout << "Durchschnittliche Temperatur: " << sum / vTemps.size() << '\n';

    sort( vTemps.begin(), vTemps.end() );
    const auto midway = vTemps.size() / 2;
    if ( vTemps.size() % 2 )
        cout << "Temperatur-Median: " << vTemps.at( midway ) << '\n';
    else
        cout << "Temperatur-Median: " << (vTemps.at(midway - 1) + vTemps.at(midway)) / 2 << '\n';

    return 0; 
}
Here is a picture of the output:

https://www.bilder-upload.eu/bild-1f83ee-1557062316.png.html



If i run the program and enter some values it prints the number of values at the beginning, before the cout-command steps in. That shouldn't be like that.

On the other hand i don't rly unterstand what the if-condition, at the end of the source, expresses. It doesn't matter which values be entered - always the else-condition is running.

Last edited on
The number of values at the beginning?
That is exactly what you do:
1
2
cout << vTemps.size();
cout << "Durchschnittliche Temperatur: ... 


How many values do you enter? In the shown example you did give 7 values.

How about "42 1 7 6"? That is four values.


7 % 2 == 1, because 3*2+1 == 7 4 % 2 == 0, because 2*2+0 == 4

The % returns the remainder of division. If you have even number of values, then size() % 2 is 0, because all even values are multiples of 2; no remainder.


PS. your else-branch does not print newline at end, unlike the other branch.
I get it slowly.

For example: if i have four values: " 7 8 9 10".

To calculate the median i must take two mean values, add both together and divide the result by 2, right?
The result of dividing is my median.

For the example means that i got 8 and 9 as mean values.

8 + 9 = 17 ; 17 / 2 = 8.5 <- this should be my median?
Topic archived. No new replies allowed.