Browse Source

Merge branch 'average_median' into 'master'

Average median

See merge request fdai8031/cstools101!12
master
fdai7968 11 months ago
parent
commit
3219663ec3
  1. 0
      src/average_median/.gitkeep
  2. 20
      src/average_median/average_median.c
  3. 9
      src/average_median/average_median.h
  4. 0
      test/average_median/.gitkeep
  5. 16
      test/average_median/test_average_median.c

0
src/average_median/.gitkeep

20
src/average_median/average_median.c

@ -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];
}
}

9
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

0
test/average_median/.gitkeep

16
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));
}
Loading…
Cancel
Save