Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Mathematics ProgramRSS Feeds

PROGRAM TO SORT A LIST AND FIND ITS MEDIAN

Posted By: Gustavo Costa     Category: C Programming     Views: 13919

WRITE A PROGRAM TO SORT A LIST AND FIND ITS MEDIAN.

Code for PROGRAM TO SORT A LIST AND FIND ITS MEDIAN in C Programming

#define N 10                                                
                                                               
   main( )                                                     
   {                                                           
       int i,j,n;                                              
       float median,a[N],t;                                    
                                                               
       printf("Enter the number of items\n");                  
       scanf("%d", &n);                                        
    /* Reading items into array a  */
printf("Input %d values \n",n); for (i = 1; i <= n ; i++) scanf("%f", &a[i]); /* Sorting begins */
for (i = 1 ; i <= n-1 ; i++) { /* Trip-i begins */
for (j = 1 ; j <= n-i ; j++) { if (a[j] <= a[j+1]) { /* Interchanging values */
t = a[j]; a[j] = a[j+1]; a[j+1] = t; } elsecontinue ; } } /* sorting ends */
/* calculation of median */
if ( n % 2 == 0) median = (a[n/2] + a[n/2+1])/2.0 ; else median = a[n/2 + 1]; /* Printing */
for (i = 1 ; i <= n ; i++) printf("%f ", a[i]); printf("\n\nMedian is %f\n", 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 Enter 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
  
Share: 


Didn't find what you were looking for? Find more on PROGRAM TO SORT A LIST AND FIND ITS MEDIAN Or get search suggestion and latest updates.

Gustavo  Costa
Gustavo Costa author of PROGRAM TO SORT A LIST AND FIND ITS MEDIAN is from Salvador, Brazil.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!