1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
# include <iostream>
using namespace std;
double median(int arr[], int size);
int main()
{
int arr[10]={1,2,3,4,5,6,7,8,9,10}; // even
int arry[9]={1,2,3,4,5,6,7,8,9}; // odd
double oddMedian;
double evenMedian;
evenMedian = median(arr,10);
oddMedian = median(arry,9);
cout << "The median of odd number array is: " << oddMedian << endl;
cout << "The median of even number array is: " << evenMedian << endl;
system("pause");
return 0;
}
double median(int arr[],int size)
{
double finalMedian;
if(size % 2 != 0)
{
finalMedian = arr[size/2];
}
else
{
finalMedian = (arr[size/2]+arr[size/2+1])/2.0;
}
return finalMedian;
}
|