Skip to content

Commit

Permalink
Close smuos#3 calculate median
Browse files Browse the repository at this point in the history
  • Loading branch information
Frozenfire92 committed Sep 23, 2014
1 parent f16654a commit 75bf591
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions mm.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ float mean (int *numbers, int length) {
return sum / length;
}

// Calculuate the Median, assumes array is sorted
float median (int *numbers, int length) {
int middleIndex = length / 2;
if (length % 2 == 0)
return (numbers[middleIndex - 1] + numbers[middleIndex]) / 2.0;
else return numbers[middleIndex];
}

int main(int argc, char *argv[]) {

int i, length, *pt;
Expand Down Expand Up @@ -61,6 +69,10 @@ int main(int argc, char *argv[]) {
float calculatedMean = mean(pt, length);
fprintf(stdout, "\n%s: Mean is: %.3f", argv[0], calculatedMean);

// Print Median
float calculatedMedian = median(pt, length);
fprintf(stdout, "\n%s: Median is: %.3f", argv[0], calculatedMedian);

fprintf(stdout, "\n%s: FIN. \n", argv[0]);

return 0;
Expand Down

0 comments on commit 75bf591

Please sign in to comment.