I'm currently working on a problem and I've been stumped on an error for about 3 hours now. Thus, I've given up being hard headed and I'm looking to the internet to see if someone else's wealth of knowledge can help to solve my problem.
A jist of the program, it takes in input from the command line of any number of numbers. It inserts them into a vector and then performs a series of statistical analysis' on the data set.
Currently, I'm having difficulty with the quintiles function (as my teacher says or "quantiles" for those of you looking it up on Wikipedia).
Logically, my code should work. However, it's seeming to hit an out of range error two before the actual end of the vector that I've created.
Heres the function followed by the functions called inside:
string quintile(vector<double> v) {
ostringstream oss;
oss << "Quintile means" << endl;
vector<unsigned> quintile{ 0 };
// get range of quintiles and insert into vector
for (unsigned i = 1; i <= 5; ++i)
quintile.push_back((v.size() * 0.2) * i);
// get the mean for each quintile
for (unsigned i = 0; i < 5; ++i)
oss << "Q" << (i + 1) << ": " << mean_sized(v, quintile[i], quintile[i + 1]) << " (" << quintile[i] << ".." << quintile[i + 1] << ")" << endl;
return oss.str();
}
double mean_sized(vector<double> v, int first, int last) {
double mean = 0.0;
vector<double> sub;
for (unsigned i = v[first]; i < v[last]; ++i)
sub.push_back(v[i]);
return (accumulate(sub.begin(), sub.end(), mean) / sub.size());
}
Any input is much appreciated as I'm seriously stumped.. I'm going to move on to the last function and hopefully by the time I'm done someone will have graced me with the knowledge to solve that issue.
Thanks!