-2
我必須找到一種方法來排序我的向量,所以數字是爲了計算模式和數據的中位數。我不知道如何實現它。我嘗試對我的矢量進行排序,但是當我顯示它時,它仍然按照之前的順序排列。我的任務是獲得x的兩個極值,並在這些範圍內計算20個增量。然後,我將這些數字放入一個公式中,並計算這些值,並將這些值連續存儲在一個向量中。然後我必須找到最大/最小值,中位數,模式,範圍和平均值。我找到了一種方法來計算除了模式和中位數之外的所有數據,我相信這個數據庫需要排序。任何幫助試圖找到中位數和模式,以及讚賞。如何排序向量以查找中位數和模式?
#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;
}