Public Group

# Computing Vector Quartiles

This topic is 3685 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I wrote this for an exercise in chapter 3 of Accelerated C++. It should print the quartiles of the vector. However when there's an even number of elements in the vector it just prints "0" for every quartile. I've been trying to get this right for days. Tried rewriting it many times etc, but it never works like intended. What's wrong? Also, I don't quite get how vectors are built up... how they really "look" like. I can't picture them. Would someone be kind enough to explain them to me? Here is the program.
#include <vector>
#include <iostream>
#include <string>
#include <iomanip>
#include <ios>
#include <algorithm>

using std::cout;			using std::vector;
using std::endl;			using std::string;
using std::setprecision;	        using std::streamsize;
using std::fixed;			using std::sort;

int main() {
// create vector
// and fill it with values
vector<int> my_vector;

my_vector.push_back(27);
my_vector.push_back(5);
my_vector.push_back(15);
my_vector.push_back(9);
my_vector.push_back(3);
my_vector.push_back(108);
my_vector.push_back(20);
my_vector.push_back(84);
my_vector.push_back(66);
//my_vector.push_back(66); // 10th element

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

typedef vector<int>::size_type vint_size;
vint_size size = my_vector.size();
vint_size mid = my_vector.size() / 2;

if (size % 2 == 0)
mid = (my_vector[mid] + my_vector[mid - 1] / 2);

// compute quartiles
double q1 = my_vector[mid / 2];			// q1 = 25th
double q2 = my_vector[mid];				// q2 = 50th = median
double q3 = my_vector[mid + mid / 2];	// 75th

// print quartiles
// invariant: we have printed i number of
// members of my_vector
streamsize prec = cout.precision();
cout << fixed << setprecision(5) << q1 << endl << q2 << endl << q3 << setprecision(prec) << endl;

for (int i = 0; i < size; i++)
cout << "Vector pos " << i << ": " << my_vector << endl;

return 0;
}

##### Share on other sites
When the number of elements in your vector is even, you're calculating an incorrect value for your mid variable. You're getting mixed up between using mid as a value and as an index.

Follow through the logic of your program carefully, especially of how you're using your mid variable. Firstly, you set it to be 1/2 the size of your vector (so, it's an index to the median value). But if you have an even number of elements, you then set mid to be equal to the median value itself!

If you don't understand how a vector<> looks like, maybe this diagram will help:
       0   1   2   3   4   5   6   7   8     +---+---+---+---+---+---+---+---+---+     | 27| 5 | 15| 9 | 3 |108| 20| 84| 66|     +---+---+---+---+---+---+---+---+---+       ^                               ^ First elmement                    Last element   (Index 0)                         (Index 8)

Your vector<int> is just a contiguous sequence of values. Using the [] operator, you can retrieve values at any index. For example in the above vector, calling my_vector[0] will return 27. my_vector[4] will return 3. my_vector[8] will return 66. And so on. (this is using your unsorted values, of course. After sorting, the order of values in the vector will be different)

Now think about what you're doing with mid. If you make mid equal to the median value, then use that to try and index into your vector, you're probably going to be accessing outside of the bounds of the vector!

##### Share on other sites
Thanks mate! :D That cleared the problem out.

1. 1
2. 2
3. 3
frob
14
4. 4
5. 5
Rutin
12

• 12
• 9
• 57
• 14
• 15
• ### Forum Statistics

• Total Topics
632112
• Total Posts
3004176

×