From 5fc05dd16e7600a333841e0bf0c434108f1b0830 Mon Sep 17 00:00:00 2001 From: Divye Date: Mon, 29 Sep 2014 16:07:19 +0000 Subject: [PATCH] Fixes #3, Added a function median() to mm.c to calculate the median. --- mm.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/mm.c b/mm.c index 51c6411..34e38d8 100644 --- a/mm.c +++ b/mm.c @@ -11,7 +11,17 @@ int numcmp (const void *a, const void *b) { if (x < y) return -1; return 0; } - +int median (int length, int *pt) +{ + int median = 0; + if(length % 2 == 0) + { + median = (pt[length/2] + pt[length/2-1])/2; + } + else + median = pt[(length-1)/2]; + return median; +} int mean (int length, int *pt) { int i; @@ -49,16 +59,9 @@ int main(int argc, char *argv[]) { pt[i] = (int) strtol(argv[i+1], NULL, 10); } - // Sort numbers - qsort(pt, length, sizeof(int), numcmp); - - // Print out numbers - fprintf(stdout, "%s: Sorted output is: \n", argv[0]); - for (i=0; i