From 252a4bc26125772f38862813e2c202a7f3f3c0e6 Mon Sep 17 00:00:00 2001 From: JINGJING TANG Date: Fri, 26 Sep 2014 21:53:22 -0300 Subject: [PATCH] Resolve issue #3: Add function median() --- mm | Bin 8808 -> 8848 bytes mm.c | 17 +++++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/mm b/mm index 37fc8d2d937860af782befc9762824fee5e289e8..b3165213714e5fbea6fe46d04e63ead73ff33a00 100755 GIT binary patch delta 1162 zcmZ8hT}TvB6uz@N8=9_n)EI&F|7JbgQmD)@!>fNyB`@e$salQf;8((4!)h4qhohXaGD+iE2T2iUR@%@0HzB zjE~5EWgU;7a5j|=-hb8`XuP!&amdH zse>!&SEO2gGd;hs8SaEKM~f_68D(UZGG!F$zhKH)vY7*cED#zTol&GXGY=` zM91M{W(RJP{1&N=#3>8J*xnFK`d;FT&YXV5JYxZ!vc4GAf^*uuK4{Vj&=a_EnzG>B zsH6|U$3U6yt5LLSn=!4;Yvh$aWs=lw(ln521u5*yH%OIF4Ckk;9_}ksBNMqWNin3r zodF$QVVsdoI{2=(X$HG_OGZP#i!3FuLYA6lI)xNt_Z^Xm>Z$f)0?HvRxJk9XRTkXT zh_0e9KMLduf{Vn(*zu;Br1vtkNqXB2+iBP? zuxG=0CE#l6l3tbT8IMvfbai;HcC>f%+ql#zmhUJu5<1CWVlV%O%kyH^nypy=r{(_$ zKUSl>!6U8mJf z2TxtJ!>2Js=y`fD0P_0u2f@o&W#< delta 919 zcmZ8gUr1A76#wr2n7KRe-LP1Z?GLO>3lv%i%QmSmz9K6SMMuI$L`7f<2_aZWN?k6K zZRep!>7lnlioNvY$c1}|GU#!{Eu?NRWFQ2Au+F#p4Qz*d&hMVz`Ofd2dv}(h=B3P* zSCt8o2qAB67|>KX`{RPA*lw{tizG$plVYB-VEYAIhIwQ=6zl90!FIqGWOIULg3UqD z7V{W_ZRi?n)3)c5bPxX8FGl!LJ_RqTK|)-JRm8`L{Qf5Tesf^rfN6(cDnQl|Q&;oe zp6`!(cV4f<;>(%YJQ$8H+msA#&gR6n zO}A-<=|hIzF^`k}Bv6rk4+2(AA2OLfV=!&5q+7hR|j|K1Vlq4 F{{UNC$(;ZI diff --git a/mm.c b/mm.c index 1aedc7f..6e4c82c 100644 --- a/mm.c +++ b/mm.c @@ -5,6 +5,7 @@ // Function declaration float mean(int length, int pt[]); +float median(int length, int pt[]); // Comparison function for qsort() int numcmp (const void *a, const void *b) { @@ -50,14 +51,16 @@ int main(int argc, char *argv[]) { fprintf(stdout, "%d ", pt[i]); } fprintf(stdout, "\nThe mean is: %f", mean(length, pt)); + fprintf(stdout, "\nThe median is: %f", median(length, pt)); fprintf(stdout, "\n%s: FIN. \n", argv[0]); return 0; } -float mean (int length, int pt[]) +float mean(int length, int pt[]) { - int i, sum, mean; + int i, sum; + float mean; sum = 0; for (i = 0; i < length; i++) { sum += pt[i]; @@ -65,3 +68,13 @@ float mean (int length, int pt[]) mean = sum/(float)length; return mean; } + +float median(int length, int pt[]) +{ + float median; + if (length % 2 == 0) + median = (float)(pt[length/2 - 1] + pt[length/2])/2; + else + median = pt[length/2]; + return median; +}