From b0eddc9d5de8cac31bcc5b0a3b859460408b437c Mon Sep 17 00:00:00 2001 From: Xujie Zheng Date: Thu, 25 Sep 2014 19:19:52 -0300 Subject: [PATCH] fixed #3 --- mm.c | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/mm.c b/mm.c index fd97905..98d60ae 100644 --- a/mm.c +++ b/mm.c @@ -21,7 +21,36 @@ int mean(int* list, int length){ } m = m/length;//the mean is equal to the total amount divided by how many term - return m; + return (int)m; +} + +int median(int* list, int length){ + //medium is the middle letter in the ordered array of numbers + //if the length is even number,the (mean of length/2 and length/2+1)th one is the medium + //if the length is odd number, the (length/2)th one is the medium since length is a interger + //which means it will always roundup + + int* md;//md is for the even number case + + if(length%2!=0){//means there is a remainder = a odd number + return list[length/2]; + } + else{ + if ((md = malloc(length * sizeof(int))) == NULL) { + fprintf(stderr, "Could not allocate memory for calculating the medium of even amount of numbers.\n"); + } + md[0]=list[length/2]; + md[1]=list[length/2-1]; + + /*the following part serve testing purposes: + int i;//for testing purposes + for (i=0; i