I do assignments for the book by Bjarne Straustrup Programming: Principles and Practice Using C ++, 2nd Edition. At the head of 4, the task 16 it is necessary to find the mode of the number series. There were no problems with one mode, but what to do when there are several modes in a number series? For example, if in the row 4, 8, 8, 4, 9; fashion is the same and 4 and 8.
vector<double> numbers; for (double n; cin >> n;) numbers.push_back(n); sort(numbers); int counter = 0; for (int i = 0; i < numbers.size(); ++i) { for (int j = 0; j < numbers.size(); ++j) { if (numbers[i] == numbers[j]) ++counter; } //ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΊΠ°ΠΆΠ΄ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ ΠΏΠΎΠ²ΡΠΎΡΡΠ΅ΡΡΡ ΡΠ°Π· arr.push_back(counter); counter = 0; } int n = 0; double max = 0; for (int i = 0; i < arr.size(); ++i) { if (arr[i] == 1) ++counter; if (arr[i] > max) { max = arr[i]; n = i; } } if (counter == arr.size()) cout << "We dont have mode\n"; else cout << "Mode: " << numbers[n] << '\n'; keep_window_open("~"); return 0;