waltermilner

Untitled

Mar 28th, 2016
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.53 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. int compare (const void * a, const void * b)
  6. {
  7.     return ( *(int*)a - *(int*)b );
  8. }
  9.  
  10. int main()
  11. {
  12.     int data[]= {9,8,7,10};
  13.     const int LENGTH=4;
  14.     qsort(data, LENGTH, sizeof(int), compare);
  15.     for (int i=0; i<LENGTH; i++)
  16.         printf("%d %d\n",i, data[i]);
  17.     if (LENGTH%2) // length is odd
  18.         printf("%d\n", data[LENGTH/2]);
  19.     else // length is even - take average of middle two
  20.         printf("%f\n", (data[LENGTH/2-1]+data[LENGTH/2])/2.0);
  21.     return 0;
  22. }
Advertisement
Add Comment
Please, Sign In to add comment