From b1586472b6e6cabaa140e8bd60e9b274fd4df93d Mon Sep 17 00:00:00 2001 From: Matthew Musoke Date: Tue, 30 Sep 2014 16:45:49 -0300 Subject: [PATCH] fixed #3 Added the median function --- mm.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/mm.c b/mm.c index 1c9c3a5..ec402aa 100644 --- a/mm.c +++ b/mm.c @@ -11,6 +11,19 @@ float mean(int length, int *pt) { return ((float)total/length); } +float median(int length, int *pt) { + if(length%2==1) return pt[length/2]; + else { + int *pt2; + if((pt2 = malloc(2*sizeof(int)))==NULL) { + fprintf(stderr, "Could not allocate memory.\n"); + } + pt2[0] = pt[length/2-1]; + pt2[1] = pt[length/2]; + return mean(2, pt2); + } +} + // Comparison function for qsort() int numcmp (const void *a, const void *b) { int x = *((int*) a); @@ -54,7 +67,8 @@ int main(int argc, char *argv[]) { for (i=0; i