Friday 3 January 2014

c++ program to find median of an array

#include<iostream.h>
#include<conio.h>
#define MAX 100
void main()
{
int arr[MAX],n;
static int count;//auto-initialise to zero
clrscr();
cout<<"Enter the size of array:";
cin>>n;
cout<<"Enter the elements of array";
for(int i=0;i<n;i++)
{
cin>>arr[i];
count++;//finds length of  array
}
cout<<"length of array is:"<<count<<"\n";
if(count%2==0)//if length of an array is even
{
 cout<<"Median value is:"<<(arr[(count/2)-1]+arr[(count/2)])/2;
}
else//if length is odd
{
cout<<"Median value is:"<<arr[count/2];
}
getch();
}

No comments:

Post a Comment