>Can someone please help me with a statement to calculate the median when the array is even.
Take the median of the two middle items: a[size / 2] and a[size / 2 + 1].
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>>Can someone please help me with a statement to calculate the median when the array is even.
>Take the median of the two middle items: a[size / 2] and a[size / 2 + 1].
Wouldn't that be the mean of a[size / 2] and a[size / 2 - 1]?
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
>>Can someone please help me with a statement to calculate the median when the array is even.
>Take the median of the two middle items: a[size / 2] and a[size / 2 + 1].
Wouldn't that be the mean of a[size / 2] and a[size / 2 - 1]?
Feh, you're no fun. ;)
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
sorry again it will b ((x[n]/2)+(x[n+1])/2)/2
Really?
#include <iostream>
float median(int* x, int n)
{
return ((x[n]/2)+(x[n+1])/2)/2;
}
int main()
{
int i, A[] = {1,2,3,4,5,6,7,8,9,10}, size = sizeof A / sizeof *A;
float result = median(A, size);
for ( i = 0; i < size; i++ )
{
std::cout << A[i] << ' ';
}
std::cout << "\nThe median of the data entered is: " << result << '\n';
return 0;
}
/* my output
1 2 3 4 5 6 7 8 9 10
The median of the data entered is: 1.38446e+06
*/
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Uh... look up .
Oh nevermind.
float median(int* x, int n)
{
return ( x [ n / 2 ] + x [ n / 2 - 1] ) / 2.0F;
}
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314