diff --git a/src/average_median/.gitkeep b/src/average_median/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/average_median/average_median.c b/src/average_median/average_median.c new file mode 100644 index 0000000..8fb03c8 --- /dev/null +++ b/src/average_median/average_median.c @@ -0,0 +1,20 @@ +#include "average_median.h" +#include +#include +#include + +double average(const double *data, int n) { + double sum = 0.0; + for (int i = 0; i < n; ++i) { + sum += data[i]; + } + 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]; + } +} diff --git a/src/average_median/average_median.h b/src/average_median/average_median.h new file mode 100644 index 0000000..b9d773f --- /dev/null +++ b/src/average_median/average_median.h @@ -0,0 +1,9 @@ +#ifndef average_median_H +#define average_median_H + +double average(const double *data, int n); +double median(double *data, int n); + +#endif + + diff --git a/test/average_median/.gitkeep b/test/average_median/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/test/average_median/test_average_median.c b/test/average_median/test_average_median.c new file mode 100644 index 0000000..8cd5e65 --- /dev/null +++ b/test/average_median/test_average_median.c @@ -0,0 +1,16 @@ +#include "unity.h" +#include "average_median.h" + +void setUp(void){} + +void tearDown(void){} + +void test_average() { + double data[] = {1.0, 2.0, 3.0, 4.0, 5.0}; + TEST_ASSERT_EQUAL_DOUBLE(3.0, mean(data, 5)); +} + +void test_median() { + double data[] = {1.0, 2.0, 3.0, 4.0, 5.0}; + TEST_ASSERT_EQUAL_DOUBLE(3.0, median(data, 5)); +}