Hi,

I'm writing this program to calculate quartiles. The error message is that the vector subscript is out of range; however, I have not been able to pinpoint where it is. Could you please help? This program is Exercise 3.2 from Accelerated C++. Thank you very much!

PHP Code:

`/*`

Write a program to compute & print the quartiles of a set of integers

Method used in this program:

(1) Use the median to divide the ordered data set into two halves.

Do not include the median into the halves.

(2) The lower quartile value is the median of the lower half of the data.

The upper quartile value is the median of the upper half of the data.

*/

#include <algorithm>

#include <iomanip>

#include <ios>

#include <iostream>

#include <string>

#include <vector>

using std::cin; using std::sort;

using std::cout; using std::streamsize;

using std::endl; using std::string;

using std::setprecision; using std::vector;

int main()

{

// ask for a set of integer values

cout << "Enter some integer values: ";

vector<int> numbers;

int x;

while (cin >> x)

numbers.push_back(x);

// check that the user entered some integer values

typedef vector<int>::size_type vector_size;

vector_size size = numbers.size();

if (size == 0) {

cout << endl << "You must enter at least one integer value. "

<< "Please try again." << endl;

return 1;

}

// sort the numbers

sort(numbers.begin(), numbers.end());

// median, 1st quartile, 3rd quartile, min, max

vector_size mid = size / 2;

double min, max, median, lquartile, uquartile;

min = numbers[0];

max = numbers[size-1];

median = size % 2 == 0 ? (numbers[mid] + numbers[mid-1])/2.0 : numbers[mid];

// break the vector into halves

vector<int> numbers1; // vector containing lower quartile

vector<int> numbers2; // vector containing upper quartile

if (size % 2 == 0) {

for (int i = 0; i < mid-1; ++i) {

numbers1.push_back(numbers[i]);

}

for (int j = mid+1; j <= size; ++j) {

numbers2.push_back(numbers[j]);

}

}

else {

for (int i = 0; i < mid; ++i) {

numbers1.push_back(numbers[i]);

}

for (int j = mid+1; j <= size; ++j) {

numbers2.push_back(numbers[j]);

}

}

// sort the two vectors

sort(numbers1.begin(), numbers1.end());

sort(numbers2.begin(), numbers2.end());

vector_size size1 = numbers1.size();

vector_size mid1 = size1 / 2;

vector_size size2 = numbers2.size();

vector_size mid2 = size2 / 2;

lquartile = size1 % 2 == 0 ? (numbers1[mid1] + numbers1[mid1-1])/2 : numbers1[mid1];

uquartile = size2 % 2 == 0 ? (numbers2[mid2] + numbers2[mid2-1])/2 : numbers2[mid2];

// print the results

cout << "Median = " << median << endl;

cout << "Min = " << min << endl;

cout << "Max = " << max << endl;

cout << "Lower quartile = " << lquartile << endl;

cout << "Upper quartile = " << uquartile << endl;

return 0;

}