Please write some pure C code to find the median value in an array.
- User
Please write some pure C code to find the median value in an array.
Assistant
Here is some C code to find the median value in an array:
cint findMedian(int arr[], int n) { // Sort the array qsort(arr, n, sizeof(int), compare); // Check if n is even or odd if (n % 2 == 0) { // If n is even, return average of middle two elements return (arr[n/2] + arr[n/2 - 1]) / 2; } else { // If n is odd, return middle element return arr[n/2]; } } // Comparison function for qsort() int compare(const void *a, const void *b) { return (*(int*)a - *(int*)b); }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.