0% found this document useful (0 votes)
27 views2 pages

Implement and Analyse To Find The Median Element in An Array of Integers

The document describes a C program to find the median element in an array of integers. The program takes user input for the number of elements and each element value. It then calls the median function, passing the number of elements and array, to calculate and return the median. The median function sorts the array in ascending order using nested for loops and a temporary variable. It then returns the middle element or the average of the two middle elements depending on if the number of elements is even or odd.

Uploaded by

Diksha Kashyap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views2 pages

Implement and Analyse To Find The Median Element in An Array of Integers

The document describes a C program to find the median element in an array of integers. The program takes user input for the number of elements and each element value. It then calls the median function, passing the number of elements and array, to calculate and return the median. The median function sorts the array in ascending order using nested for loops and a temporary variable. It then returns the middle element or the average of the two middle elements depending on if the number of elements is even or odd.

Uploaded by

Diksha Kashyap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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