function array median

#include <stdio.h>
#include <stdlib.h>

// Function to compare integers for qsort
int compare(const void a, const void b) {
    return ((int)a - (int)b);
}

// Function to find median of an array
float findMedian(int arr[], int size) {
    // Sort the array
    qsort(arr, size, sizeof(int), compare);

    // Find the middle index
    int middle = size / 2;

    // If the size is even, average the middle two elements
    if (size % 2 == 0) {
        return (float)(arr[middle - 1] + arr[middle]) / 2;
    }
    // If the size is odd, return the middle element
    else {
        return (float)arr[middle];
    }
}

// Main function
int main() {
    // Example array
    int arr[] = {5, 2, 9, 1, 7, 6};

    // Calculate the size of the array
    int size = sizeof(arr) / sizeof(arr[0]);

    // Find and print the median
    printf("Median: %f\n", findMedian(arr, size));

    return 0;
}