From 8b9a66f392c4af04087b489b6982fee00e8fdffc Mon Sep 17 00:00:00 2001 From: Xujie Zheng Date: Thu, 25 Sep 2014 19:14:20 -0300 Subject: [PATCH 1/2] fixed #3 --- mm.c | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/mm.c b/mm.c index fd97905..242375a 100644 --- a/mm.c +++ b/mm.c @@ -21,7 +21,34 @@ 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]; + + int i;//for testing purposes + for (i=0; i Date: Thu, 25 Sep 2014 19:15:51 -0300 Subject: [PATCH 2/2] fixed #3 --- mm.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mm.c b/mm.c index 242375a..98d60ae 100644 --- a/mm.c +++ b/mm.c @@ -41,11 +41,13 @@ int median(int* list, int length){ } 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