Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 1. Write a program to take some numbers into an array and calculate median of the array
- elements.
- */
- #include<stdio.h>
- int main()
- {
- int n, tmp, max, i, j;
- // array size
- scanf("%d", &n);
- int ar[n];
- // input array
- for (i = 0; i < n; ++i)
- scanf("%d", &ar[i]);
- // sort
- for (i = 0; i < n-1; ++i)
- {
- max = i;
- for (j = i+1; j < n; ++j)
- if(ar[j] < ar[max])
- max = j;
- tmp = ar[i];
- ar[i] = ar[max];
- ar[max] = tmp;
- }
- if (n&1)
- printf("%d\n", ar[(n-1)/2]);
- else
- printf("%.2f\n", float(ar[(n+1)/2-1]+ar[(n+1)/2])/2.0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment