Skip to content

Commit

Permalink
Fix smuos#3: Make mm.c fork(), parent call mean() and child call medi…
Browse files Browse the repository at this point in the history
…an()
  • Loading branch information
zekewang918 committed Sep 27, 2014
1 parent ce7d277 commit e479d5f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 8 deletions.
Binary file modified mm
Binary file not shown.
31 changes: 26 additions & 5 deletions mm.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#define debug 0
#define FAILED 1

// Declare Function
int numcmp (const void *a, const void *b);
float mean(int *num, int length);
float median(int *num, int length);

// Comparison function for qsort()
int numcmp (const void *a, const void *b) {
Expand Down Expand Up @@ -30,12 +37,12 @@ float mean(int *num, int length) {
// Median function that outputs median
float median(int *num, int length){
// If the length of array is even
if(length%2==0){
if (length%2==0){
//return the average number between two median numbers
return (num[length/2-1]+num[length/2])/(float)2;
// If the length of array is odd
}else{
// Return the median
// Return the median
return num[length/2+1];
}
}
Expand Down Expand Up @@ -74,10 +81,24 @@ int main(int argc, char *argv[]) {
for (i=0; i<length; i++) {
fprintf(stdout, "%d ", pt[i]);
}

// Initilaztion fork() for process
int rc = fork();

fprintf(stdout, "\nMean: %f", mean(pt, length));
fprintf(stdout, "\nMedian: %f", median(pt, length));
fprintf(stdout, "\n%s: FIN. \n", argv[0]);
// If there is no arugment
if (rc < 0){
fprintf(stderr, "Fork Failed %s\n", argv[0]);
exit(FAILED);
// If the process is child
} else if (rc == 0){
fprintf(stdout, "This is child process(pid:%d)\n", (int)getpid());
fprintf(stdout, "The median is: %f", median(pt, length));
// If the process is parent
} else if (rc > 0){
fprintf(stdout, "This is parent prcesss(pid:%d)", (int)getpid());
fprintf(stdout, "The mean is: %f", mean(pt, length));
fprintf(stdout, "\n%s: FIN. \n",argv[0]);
}

return 0;

Expand Down
40 changes: 37 additions & 3 deletions mm.c~
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#define debug 0
#define FAILED 1

// Declare Function
int numcmp (const void *a, const void *b);
float mean(int *num, int length);
float median(int *num, int length);

// Comparison function for qsort()
int numcmp (const void *a, const void *b) {
Expand All @@ -12,19 +19,30 @@ int numcmp (const void *a, const void *b) {
return 0;
}

//Mean functiont that outputs mean of an array
float mean(int *num, int length) {
// Initialization of vars
// sum -> sum of ints of array
// i -> num used for doing iteation
float sum = 0;
int i;
// For loop to add every number to sum
for(i=0; i<length;i++){
sum += num[i];
}
// Return mean
return sum/length;
}

// Median function that outputs median
float median(int *num, int length){
if(length%2==0){
// If the length of array is even
if (length%2==0){
//return the average number between two median numbers
return (num[length/2-1]+num[length/2])/(float)2;
// If the length of array is odd
}else{
// Return the median
return num[length/2+1];
}
}
Expand Down Expand Up @@ -63,9 +81,25 @@ int main(int argc, char *argv[]) {
for (i=0; i<length; i++) {
fprintf(stdout, "%d ", pt[i]);
}

// Initilaztion fork() for process
int rc = fork();

fprintf(stdout, "\nMean: %f", mean(pt, length));
fprintf(stdout, "\nMedian: %f", median(pt, length));
// If there is no arugment
if (rc < 0){
fprintf(stderr, "Fork Failed %s\n", argv[0]);
exit(FAILED);
// If the process is child
} else if (rc == 0){
fprintf(stdout, "This is child process(pid:%d)\n", (int)getpid());
fprintf(stdout, "The median is: %f", median(pt, length));
// If the process is parent
} else if (rc > 0){
fprintf(stdout, "This is parent prcesss(pid:%d)", (int)getpid());
fprintf(stdout, "The mean is: %f", mean(pt, length));
}

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

return 0;
Expand Down

0 comments on commit e479d5f

Please sign in to comment.