In statistics the median of a set of values is the value that lies in the middle
ID: 3639884 • Letter: I
Question
In statistics the median of a set of values is the value that lies in the middle when the values are arranged in sorted order. If the set has an even number of values, then the median is taken to be the average of the two middle values. Write a function that determines the median of a sorted array. The function should take an array of numbers and an integer indicating the size of the array and return the median of the values in the array. You may assume the array is already sorted. Use pointer notation whenever possible.
Explanation / Answer
#include<iostream.h>
int main()
{
int *arr;
int n,median,i;
cout<<"Enter no.of values in array";
cin>>n;
arr = (int*)malloc(n*sizeof(int));
cout<<"enter the array values in sorted order";
for(i=0;i<n;i++)
cin>>arr[i];
if(n%2==0)
{
i = n/2;
median = (arr[i]+arr[i-1])/2;
}
else
{
i = (n-1)/2;
median = arr[i];
}
cout << "Median is " << median;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.