C++ 如何对向量进行排序以找到中间值和模式?
我必须找到一种方法对向量进行排序,以便计算数据的模式和中值。我不知道如何实施它。我试着对向量进行排序,但当我显示它时,它仍然是以前的顺序。我的任务是得到x的两个极值,并在这些范围内计算20个增量。然后,我将这些数字放入一个方程式中,计算出这些值,并将这些值连续存储在一个向量中。然后我必须找到最大/最小值、中值、模式、范围和平均值。我已经找到了一种方法来计算除mode和median之外的所有向量,我认为向量需要排序。任何试图找到中位数和模式的帮助都将不胜感激C++ 如何对向量进行排序以找到中间值和模式?,c++,sorting,vector,C++,Sorting,Vector,我必须找到一种方法对向量进行排序,以便计算数据的模式和中值。我不知道如何实施它。我试着对向量进行排序,但当我显示它时,它仍然是以前的顺序。我的任务是得到x的两个极值,并在这些范围内计算20个增量。然后,我将这些数字放入一个方程式中,计算出这些值,并将这些值连续存储在一个向量中。然后我必须找到最大/最小值、中值、模式、范围和平均值。我已经找到了一种方法来计算除mode和median之外的所有向量,我认为向量需要排序。任何试图找到中位数和模式的帮助都将不胜感激 #include <iostre
#include <iostream>
#include <iomanip>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
double xmin, xmax;
const int POINTS = 20;
const double PI = 3.1416;
double increments;
int counter = 0;
double range, mean;
double total = 0;
vector<vector<double> > values;
cout << "Enter in a value for the minimum x value: ";
cin >> xmin;
cout << "Enter in a value for the maximum x value: ";
cin >> xmax;
if (xmin < 0)
increments = (abs(xmin) + xmax) / POINTS;
else
increments = (xmax - xmin) / POINTS;
double x = xmin + increments * counter;
double min = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x);
double max = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x);
cout << setw(15) << "x |" << setw(15) << "f(x)" << endl;
cout << setw(32) << setfill('-') << " " << endl;
cout << setfill(' ');
vector<double> auxiliar;
while (x <= xmax)
{
auxiliar.resize(2);
auxiliar[0] = x;
auxiliar[1] = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x);
values.push_back(auxiliar);
auxiliar.clear();
if (0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x) > max)
max = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x);
if (0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x) < min)
min = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x);
counter++;
x = xmin + increments * counter;
}
for (vector<double> auxiliar : values)
cout << fixed << showpos << setw(15) << setprecision(2) << auxiliar[0] << setw(15) << setprecision(4) << auxiliar[1] << endl;
cout << endl;
range = max - min;
for (vector<double> auxiliar : values)
{
total += auxiliar[1];
}
mean = total / POINTS;
sort(auxiliar.begin(), auxiliar.end());
cout << "The maximum value is: " << max << endl;
cout << "The minumum value is: " << min << endl;
cout << "The range is: " << range << endl;
cout << "The mean value is: " << mean << endl;
cout << "The median value is: " << endl;
cout << "The mode value is: " << endl;
system("pause");
return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
双xmin,xmax;
常数积分=20;
常数双PI=3.1416;
双倍递增;
int计数器=0;
双量程,平均值;
双倍合计=0;
向量值;
cout>xmin;
cout>xmax;
if(xmin<0)
增量=(abs(xmin)+xmax)/点;
其他的
增量=(xmax-xmin)/点;
双x=xmin+增量*计数器;
双最小值=0.0572*cos(4.667*x)+0.0218*PI*cos(12.22*x);
双最大值=0.0572*cos(4.667*x)+0.0218*PI*cos(12.22*x);
cout有一个非常简单的std函数,它将为您提供数组的中位数:std::nth\u元素
基本上,如果你想得到你的中位数,你可以这样做:
std::vector<int> v{5, 6, 4, 3, 2, 6, 7, 9, 3};
std::nth_element(v.begin(), v.begin() + v.size()/2, v.end());
std::cout << "The median is " << v[v.size()/2] << '\n';
std::向量v{5,6,4,3,2,6,7,9,3};
std::n_元素(v.begin(),v.begin()+v.size()/2,v.end());
标准::cout