Header Ad

C program to sort a list of numbers and to determine the median

In this tutorial, we are going to write a C Program to sort a list of numbers and to determine the median in C Programming with practical program code and step-by-step full complete explanation.

C Program to sort a list of numbers and to determine the median


C Program to sort a list of numbers and to determine the median.

 
#include<stdio.h>
#include<conio.h>

#define N 10

void main()
{
    int i,j,n;
    float median,a[N],t;

    printf("Enter the number of items\n");
    scanf("%d",&n);
    printf("Input %d values\n",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)
        median=(a[n/2]+a[n/2+1])/2.0;
    else
        median=a[n/2+1];

    for(i=1;i<=n;i++)
        printf("%f",a[i]);

    printf("\nMedian is %f",median);
}


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

Ente the number of items
6
Input 6 values
3 5 8 9 4 6

9.000000 8.000000 6.000000 5.000000 4.000000 3.000000
Median is 5.500000


Post a Comment

0 Comments