0

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!

  • Your teacher is correct in calling these "quintile"s as [that's the name for 5-quantiles](https://en.wikipedia.org/wiki/Quantile#Specialized_quantiles), which are what you are dealing with here. – das-g Feb 21 '15 at 00:48
  • Good to know! I was wondering why no one mentioned that the Wiki page didn't match up to the name he was using. Makes more sense now, quantiles are the individual parts of a quintile. – halamandres Feb 21 '15 at 01:10
  • Not quite. If you separate your value population into `q` subpopulations, the values "between" those subpopulations are called "q-quantiles". Thus, if `q`= 5, those are "5-quantiles". "Quintile" is just a synonym for "5-quantile". (Like "Median" is a synonym for the first (and only) 2-quantile, and "percentiles" is a synonym for "100-quantiles", so quantiles for when the population is separated into 2 or 100 subpopulations, respectively.) – das-g Feb 21 '15 at 01:21
  • Interesting, so quantile is more of a universal term used for a subset of the population and there can be n number of quantiles (which will essentially change the synonym used to reference the group). Whereas, quintile is a synonym specifically referring to a subset of 5-quantiles, correct? – halamandres Feb 21 '15 at 15:08
  • Almost. Yes, "quantile" is a more general term than "quintile". And yes, "quintile" is more specific insofar, as the number of same-size subsets is 5 for them. But both terms refer to the values "between" those subsets (the values at the boundaries), **not to the subsets** themselves. – das-g Feb 21 '15 at 15:16
  • For a better explanation of the terminology, http://math.stackexchange.com might be the place to go. – das-g Feb 21 '15 at 15:21

1 Answers1

2

This loop is wrong:

for (unsigned i = v[first]; i < v[last]; ++i)
    sub.push_back(v[i]);

You're using the values in the vector as the range of indexes. You should just be using the range from first to last:

for (unsigned i = first; i <= last; i++) {
    sub.push_back(v[i]);
}

P.S. See Why is it considered a bad practice to omit curly braces?

Community
  • 1
  • 1
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • You got it! MAN, it's always the easy ones that get away from me. I'll totally check out that link. To be honest, I only take out the curly braces because the programs I've been writing thus far aren't that complex and it doesn't seem to bother my teacher. – halamandres Feb 21 '15 at 01:08