I need to find 75th percentile of this {4,5,10,10,10,10,36,20,25,30,35,45,55,60,
ID: 3604492 • Letter: I
Question
I need to find 75th percentile of this {4,5,10,10,10,10,36,20,25,30,35,45,55,60,65} and here is my code
My program does work fine for when length%4==0, but it doesnt work when length is not a multiple of 4.
//for 75% percentile
int length=15;
int i,p3;
i=length/4; //which is 25% each
if(length%4==0){//to check whether the remainder is equal to 0
//I make i*3 to bring it to the 75th percentile, otherwise i will be the 25th percentile.
p3=(arr[i*3]+arr[(i*3)-1])/2.0;//if the remainder is equal to 0, meaning I will have to find the average of the two elements.
}
else{//if it is the odd number, the program will print out straight forward.
p3=arr[i*3];
}
Explanation / Answer
/*When you divide the length by 4 and if the length is not multiple of 4 then it will give take floor of it, which creates a problem.
For example, if length = 15 and you divide it by 4 then i will take value 15/4 = 3 and it will assign p3 = arr[3*3] = arr[9] which is wrong, Actual 75% is 15*0.75 = 11.25 => 11.
so you need to take i = 0.75*length and in condition write i inplace of 3*i */
// code in C++ is as following
#include<iostream>
using namespace std;
int main()
{
int length = 15;
int arr[] = {4,5,10,10,10,10,36,20,25,30,35,45,55,60,65};
int i,p3;
// define i as following
i = 0.75 * length; // if we define it length/4 then it take floor of
// length and don't give correct output if length is not multiple of 4
if(length%4 == 0)
{
p3 = arr[i] + arr[i-1];
}
else
{
p3 = arr[i];
}
cout << "p3 = " << p3 << endl;
}