Average median See merge request fdai8031/cstools101!12
@ -0,0 +1,20 @@
#include "average_median.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
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];
@ -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
@ -0,0 +1,16 @@
#include "unity.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() {
TEST_ASSERT_EQUAL_DOUBLE(3.0, median(data, 5));