C Program to sort a list of numbers and find median

				
					#include<stdio.h> 
#include<conio.h>
#define M 10 
void main() 
{ 
int i, j, n; 
float med, a[M], t; 
printf(“Enter the number of items”); 
scanf(“%d”, &n); 
printf(“Input %d values ”, n); 
for(i=1; i<=n; i++) 
scanf(“%f”, &a[i]); 
for(i=1; i<=n-1; i++) 
{ 
for(j=1; j<=n-1; j++) 
{ 
if(a[j]<=a[j+1]) 
t=a[j]; 
a[j]=a[j+1]; 
a[j+1]=t; 
} 
else 
continue; 
} 
} 
if(n%2 = =0) 
med=(a[n/2]+a[n/2+1])/2.0; 
else 
med=a[n/2+1]; 
for(i=1; mi<=n; i++) 
printf(“%f”, a[i]); 
printf(“median is %f”, med);
}
				
			
Output:

Enter the number of items 5

Input 5 values

1.111 2.222 3.333 4.444 5.555

5.555000    4.444000      3.333000     2.222000     1.111000

Median is 3.333000

Leave a Reply