Skip to content

Commit

Permalink
Changed format and comments for smuos#4 and smuos#5 to make sure prin…
Browse files Browse the repository at this point in the history
…t mean and median, added smuos#6 wait() to make the parent wait until child has finished
  • Loading branch information
xuwentao committed Oct 2, 2014
1 parent 1d68601 commit b98e381
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions mm.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>

#define debug 0

Expand Down Expand Up @@ -28,6 +29,7 @@ float mean (int num[], int length)
float median (int num[], int length)
{
int medianNum = length / 2;

//Determine the size of array is even or odd
if (length % 2 == 0)
{
Expand Down Expand Up @@ -89,18 +91,20 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "Fork failed. \n");
//Child process
} else if (rc == 0) {
for (i=0; i<length; i++) {
fprintf(stdout, "%d ", pt[i]);
for (i=0; i<length; i++) {
fprintf(stdout, "%d ", pt[i]);
}
//Child call median() function
fprintf(stdout, "\nThe median is: %.02f ", median (pt, length));

//Child call median() function and cout median
fprintf(stdout, "\nThe median is: %.02f ", median (pt, length));
} else {

//Wait for parent process
wait(NULL);
//Parent call mean() function
fprintf(stdout, "\nThe mean is: %.02f", mean (pt, length));

fprintf(stdout, "\n%s: FIN. \n", argv[0]);
//Parent call mean() function and cout mean
fprintf(stdout, "\nThe mean is: %.02f", mean (pt, length));
fprintf(stdout, "\n%s: FIN. \n", argv[0]);
}
return 0;
}

0 comments on commit b98e381

Please sign in to comment.