From 4409190300931dce90be799e9144c9303e32ba14 Mon Sep 17 00:00:00 2001 From: fdai7968 Date: Thu, 8 Feb 2024 18:24:12 +0000 Subject: [PATCH] code for median --- src/average_median/average_median.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/average_median/average_median.c b/src/average_median/average_median.c index fcb7664..cea91e0 100644 --- a/src/average_median/average_median.c +++ b/src/average_median/average_median.c @@ -10,3 +10,11 @@ double average(const double *data, int n) { } return sum / n; } + +double median(double *data, int n) { + if (n % 2 == 0) { + return (data[n / 2 - 1] + data[n / 2]) / 2.0; + } else { + return data[n / 2]; + } +}