Skip to content

Commit

Permalink
fixed smuos#3
Browse files Browse the repository at this point in the history
  • Loading branch information
xj721386 committed Sep 25, 2014
1 parent 076458c commit b0eddc9
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions mm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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<length; i++) {
fprintf(stdout, "%d ", md[i]);
}
*/

return mean(md,2);
}
}

int main(int argc, char *argv[]) {
Expand Down Expand Up @@ -58,7 +87,8 @@ int main(int argc, char *argv[]) {
for (i=0; i<length; i++) {
fprintf(stdout, "%d ", pt[i]);
}
fprintf(stdout, "\n%s: FIN. \n", argv[0]);
fprintf(stdout, "mean: %d (FYI: the mean will be round up)\n", mean(pt, length));
fprintf(stdout, "\n%s: FIN.\n", argv[0]);
fprintf(stdout, "mean: %d\nmedian:%d\nFYI: the mean will be round down\n",
mean(pt, length),median(pt,length));
return 0;
}

0 comments on commit b0eddc9

Please sign in to comment.