• 14
• 12
• 9
• 10
• 9

# Quartile

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

## Recommended Posts

Ok i need to find the Quartile of x amount of numbers now that in it self is not a problem, the problem i have is doing it effeciently say i have the numbers 4 2 1 3, in a vector, i sort them so they become 1 2 3 4 now its easy to find the middle Quartile, i can also find the first Quartile, 1.5 and 2.5, now i need the high Quartile, now here comes my problem, i dont know exatly how to do that effeciently, the only way i can think of is to resort the vector with the numbers in reverce direction, the problem is that there must be a faster way to do it. ------------------ Learning C++

##### Share on other sites
Quote:
 Original post by DevtronOk i need to find the Quartile of x amount of numbersnow that in it self is not a problem, the problem i have is doing it effecientlysay i have the numbers 4 2 1 3, in a vector, i sort them so they become 1 2 3 4now its easy to find the middle Quartile, i can also find the first Quartile, 1.5 and 2.5, now i need the high Quartile, now here comes my problem, i dont know exatly how to do that effeciently, the only way i can think of is to resort the vector with the numbers in reverce direction, the problem is that there must be a faster way to do it.------------------Learning C++

Vectors are random-access, and asking for their .size() is constant-time (they keep track of that information). So just calculate the position where the high quartile starts, and index in.

##### Share on other sites
So if I understand correctly, your question boils down to: How can I sort a std::vector of numbers in descending order?

First you can include the standard <algorithm> header for access to, you guessed it: algorithms. Then tell it to sort your container, but ensure to provide it with reverse iterators pointing to the beginning and end of the vector.

Example:

std::vector<int> v;
std::sort(v.rbegin(), v.rend());

The algorithm used for the sort is implementation dependent, but its probably Quick Sort.

[Edited by - fpsgamer on August 12, 2007 11:57:52 AM]

##### Share on other sites

I have already sorted them, and i know how to find the median of x amount of numbers, my problem is locating the low and high quartile.

now from what i figure i could take the size of the vector say it has 10 numbers in it and then devide that by 4, 10/4= 2.5 , then problem then becomes that the low quartile is 3 in this case and then my problem comes as there is nothing in the 2.5 spot in the vector, dose the vector just round of and say 2, if so if the number i had gotten was 2.99 had it still just rounded of at 2 ?

##### Share on other sites
L = {1, 2, 3, 4}
n = length(L) + 1 = 5
Li = ith element of L

Q1 = average(Lfloor(n / 4), Lceiling(n / 4))
Q2 = average(Lfloor(n / 2), Lceiling(n / 2))
Q3 = average(Lfloor(3n / 4), Lceiling(3n / 4))

(3n / 4 = (n / 4) + (n / 2))

Of course, Q3 can be gotten in the same way as Zahlman describes -- indexing from the end rather than the beginning using the index from Q1.

##### Share on other sites
Quote:
 Original post by TheUnbelieverL = {1, 2, 3, 4}n = length(L) + 1 = 5Li = ith element of LQ1 = average(Lfloor(n / 4), Lceiling(n / 4))Q2 = average(Lfloor(n / 2), Lceiling(n / 2))Q3 = average(Lfloor(3n / 4), Lceiling(3n / 4))(3n / 4 = (n / 4) + (n / 2))Of course, Q3 can be gotten in the same way as Zahlman describes -- indexing from the end rather than the beginning using the index from Q1.

my brain hurts i dont understand any of those formulas

and i have thought about reversing the index,

##### Share on other sites
Quote:
 Original post by Devtronmy brain hurts i dont understand any of those formulasand i have thought about reversing the index,

Floor and ceiling

They are provided by the cmath library in C++.

Q1-Q3 are average of either one or two values, depending on the size of the array.

##### Share on other sites
here is a question,

vector<double> numbers;

vector<double>::size_type size = numbers.size();

if i enter 3 numbers into the vector, and i ask for how many numbers are in it it will tell me there are 3 numbers in it, now if i want to devide that by 2, as fare as i know from normal math 3/2 = 1.5, but if i do that to size/2 it returns 1, so if i understand this right asking for a vector size and deviding that it will never return a .* value but only an * value.

##### Share on other sites
Integer arithmetic will give you an integer result. Floating point arithmetic will give a floating point result.

In short:

void integerArithmetic(){    int three = 3;    int two = 2;    int one = 1;    assert(three / two == one);}void floatingArithmetic(){    double three = 3.0;    double two = 2.0;    double onePointFive = 1.5;    assert(three / two == onePointFive);}

EDIT: Also worth noting is that directly comparing floating point values is usually a bad idea. See What Every Computer Scientist Should Know About Floating-Point Arithmetic.

##### Share on other sites
Quote:
 Original post by Devtronhere is a question,vector numbers;vector::size_type size = numbers.size();if i enter 3 numbers into the vector, and i ask for how many numbers are in it it will tell me there are 3 numbers in it, now if i want to devide that by 2, as fare as i know from normal math 3/2 = 1.5, but if i do that to size/2 it returns 1, so if i understand this right asking for a vector size and deviding that it will never return a .* value but only an * value.

Right. Integer arithmetic in C and C++ (and many other programming languages) simply discards the remainder. If you want a floating-point result, then at least one of the inputs needs to be a floating-point value.

This behaviour is actually very useful in many situations.

(Also, keep in mind that the type of the indices has nothing to do with the type of the elements.