Suppose we define the median of a sequence as a number, relative to which exactly half of the elements are smaller, and the second is larger. Find the median turned out, but I found another using size (). It is necessary to find the median of the even series, which I did, but I will repeat with the help of size (), which prevents me from using their value. For example: in the even sorted row {3, 6, 7, 10} the median will be {6, 7} therefore 6 + 7/2 (how do I count if I use the row numbers)?

#include <iostream> #include <vector> #include <algorithm> using std::cout; using std::cin; using std::endl; using std::vector; int main() { vector <double> items; double temps(0); double sum(0); double sum_line(0); setlocale(LC_ALL, "RUS"); cout << " Наберите ряд чисел: для получения Медианы." << endl; while (cin >> temps) items.push_back(temps); cout << " Количество элементов в векторе:" << items.size() << endl; sort(items.begin(), items.end()); for (int i = 0; i < items.size(); ++i) { cout << "[V" << i << "] = " << items[i] << endl; } if (items.size() % 2) cout << " Медиана не чётного ряда = " << items[items.size() - 1] / 2 + 1 << endl; else sum += items.size() / 2; sum_line += items.size() / 2 + 1; cout << sum << endl << sum_line << endl << " Среднее значение этих элементов " << (sum + sum_line) / 2 << endl; // тут хотел посчитать // вычислить их среднее // значение. } 
  • 3
    Nothing is clear. Please use Human Language when describing the problem. - Vlad from Moscow
  • You need to write a program to find the median from a number of random numbers dialed by the user. - its_space
  • What are sum and sum_line? How are they related to the median? - Vlad from Moscow
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

What is a median in your understanding? In the world of mat. statistics, the median is called the number X from a set, that half of the numbers are greater than X, and half less. Thus, the median for an odd number of numbers is incorrectly calculated, given that you have already sorted the array:

 cout << " Медиана не чётного ряда = " << items[items.size() - 1] / 2 + 1 << endl; 

rightly so:

 cout << " Медиана не чётного ряда = " << items[items.size() / 2] // при нечетном количестве это будет середина массива << endl; 

with an even number of elements, you can choose the median number between the two middle elements of the set:

 (items[items.size() / 2] + items[items.size() / 2 - 1]) / 2 

It is also worth considering the extreme cases (for example, they can be separately processed): items.size() == 0 и items.size() == 1

  • items.size() == 1 can be not processed separately if one expression is modified: (items.size() - 1) / 2 . - D-side
  • by itself, but in this case it does not matter where to sculpt a crutch. It seemed to me that it would be clearer. - iksuy
  • thank you you helped me a lot. I tried to use the formula to calculate the median. cout << "Median not even row =" << items [items.size () - 1] / 2 + 1 << endl; - its_space
  • one
    @iksuy in general, yes. But such a method even allows you to throw out the condition “if the size is even” :) Well, it doesn't matter, my job is to offer. - D-side
  • one
    The @avp median, by definition, can be any number between them . But this is by definition. Since for any of them the properties of the median will be satisfied, you can take any. The concepts of "lower / upper median" have their own, another definition :) - D-side