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's an example code snippet in C to find the median value in an array:
#include <stdio.h> #include <stdlib.h> double median(int* arr, int size) { // Find the middle index int middle = size / 2; // Sort the array in ascending order int temp; for (int i = 0; i < size; i++) { if (i == middle) { temp = arr[i]; } else if (arr[i] < arr[middle]) { temp = arr[i]; } else { temp = arr[middle]; } } // Return the middle index return (double)middle / 2; } int main() { int arr[] = {1, 2, 3, 4, 5}; int size = sizeof(arr) / sizeof(arr[0]); int median = median(arr, size); printf("The median value is: %d\n", median); return 0; }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.