Description
This program finds the median of n numbers in an array. Median is the middle most element in an array. If the number of elements in an array is even, then median is the average of the two middle elements. For example if an array consist of 10 elements then the median is the average of 5th and 6th element.
Example
Array1
5 6 8 9 10
Here the total number of elements is odd, so median is the middle most element 8.
Array2
9 5 4 6 2 1 4 6 3 7
Here the number of elements is even, so median is (2+1)/2 =1.5
Program
// c program to find median
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
{
int a[10],i,n;
float m;
printf("Enter the size of the array");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
if(n%2==0)
{
m=(float(a[(n/2)-1])+float(a[n/2]))/2;
}
else
{
m=a[n/2];
}
printf("\n Median is %f",m);
getch();
}
No comments:
Post a Comment