Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

PROGRAM NO: 2

Implement and analyse to find the median element in an array of


integers
#include<stdio.h>
#include<conio.h>
void main()
{
int x[100],n,i;
float mean(int,int[]);
float median(int,int[]);
clrscr();
printf("Enter the number of integers");
scanf("%d",&n);
printf("Enter the elements");
for(i=0;i<n;i++)
scanf("%d",&x[i]);
printf("median=%f\n",median(n,x));
getch();
}
float median(int n, int x[]) {
float temp;
int i, j;
for(i=0; i<n-1; i++) {
for(j=i+1; j<n; j++)
{
if(x[j] < x[i])
{
temp = x[i];
x[i] = x[j];
x[j] = temp;
}
}
}

if(n%2==0)
return((x[n/2] + x[n/2 - 1]) / 2.0);

else
{
return x[n/2];
}
}
OUTPUT

Enter the no. of elements


6

Enter the elements


4
4
3
8
6
1

Median=4.00000

You might also like