ibrahim_065

Write a program to take some numbers into an array and calculate median of the array elements.

Oct 10th, 2020 (edited)
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.57 KB | None | 0 0
  1. /*
  2. 1. Write a program to take some numbers into an array and calculate median of the array
  3. elements.
  4. */
  5. #include<stdio.h>
  6. int main()
  7. {
  8.     int n, tmp, max, i, j;
  9.     // array size
  10.     scanf("%d", &n);
  11.     int ar[n];
  12.     // input array
  13.     for (i = 0; i < n; ++i)
  14.         scanf("%d", &ar[i]);
  15.   // sort
  16.     for (i = 0; i < n-1; ++i)
  17.     {
  18.         max = i;
  19.         for (j = i+1; j < n; ++j)
  20.             if(ar[j] < ar[max])
  21.                 max = j;
  22.         tmp = ar[i];
  23.         ar[i] = ar[max];
  24.         ar[max] = tmp;
  25.     }
  26.     if (n&1)
  27.         printf("%d\n", ar[(n-1)/2]);
  28.     else
  29.         printf("%.2f\n", float(ar[(n+1)/2-1]+ar[(n+1)/2])/2.0);
  30.     return 0;
  31. }
  32.  
Advertisement
Add Comment
Please, Sign In to add comment