From 819add58d8a7fd0469d9ddbd533fba4a71831d8e Mon Sep 17 00:00:00 2001 From: jcsl94 Date: Thu, 25 Sep 2014 16:27:51 -0300 Subject: [PATCH 1/7] created new file mm.c and copied contents of sort.c over --- mm.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 mm.c diff --git a/mm.c b/mm.c new file mode 100644 index 0000000..ff1779f --- /dev/null +++ b/mm.c @@ -0,0 +1,52 @@ +#include +#include + +#define debug 0 + +// Comparison function for qsort() +int numcmp (const void *a, const void *b) { + int x = *((int*) a); + int y = *((int*) b); + if (x > y) return 1; + if (x < y) return -1; + return 0; +} + +int main(int argc, char *argv[]) { + + int i, length, *pt; + + // Check for proper usage + if (argc < 2) { + fprintf(stderr, "%s: Aborting, not enough arguments.\n", argv[0]); + return (-1); + } + + // Determine amount of numbers from argc + length = argc - 1; +#if debug + fprintf(stderr, "%s: DEBUG: %d numbers were passed.\n", argv[0], length); +#endif + + // Allocate memory for array of number (and error check) + if ((pt = malloc(length * sizeof(int))) == NULL) { + fprintf(stderr, "%s: Could not allocate memory.\n", argv[0]); + } + + // Read numbers into array + for (i = 0; i < length; i++) { + pt[i] = (int) strtol(argv[i+1], NULL, 10); + } + + // Sort numbers + qsort(pt, length, sizeof(int), numcmp); + + // Print out numbers + fprintf(stdout, "%s: Sorted output is: \n", argv[0]); + for (i=0; i Date: Thu, 25 Sep 2014 17:10:34 -0300 Subject: [PATCH 2/7] Resolved issue #2, mean function working. --- mm.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/mm.c b/mm.c index ff1779f..62643ea 100644 --- a/mm.c +++ b/mm.c @@ -3,6 +3,19 @@ #define debug 0 +//Mean calculating function +double mean(int *pt, int length) +{ + int sum = 0; + double mean = 0; + for(int i = 0; i < length; i++) + { + sum = sum + pt[i]; + } + mean = ((double)sum/length); + return mean; +} + // Comparison function for qsort() int numcmp (const void *a, const void *b) { int x = *((int*) a); @@ -48,5 +61,7 @@ int main(int argc, char *argv[]) { } fprintf(stdout, "\n%s: FIN. \n", argv[0]); + // Call mean function + fprintf(stdout, "The mean is: %.2f \n", mean(pt, length)); return 0; } From 0eea8bd9c821d04729e2389f60213967053e41d0 Mon Sep 17 00:00:00 2001 From: jcsl94 Date: Thu, 25 Sep 2014 22:08:19 -0300 Subject: [PATCH 3/7] Resolved issue #3, median function is working. --- mm.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/mm.c b/mm.c index 62643ea..188d33d 100644 --- a/mm.c +++ b/mm.c @@ -3,6 +3,14 @@ #define debug 0 +//Median calculating function +int median(int *pt, int length) +{ + int middle = (length/2); + int median = pt[middle]; + return median; +} + //Mean calculating function double mean(int *pt, int length) { @@ -63,5 +71,8 @@ int main(int argc, char *argv[]) { // Call mean function fprintf(stdout, "The mean is: %.2f \n", mean(pt, length)); + + // Call median function + fprintf(stdout, "The median is: %d \n", median(pt, length)); return 0; } From 610e28700d7e9e850f50cd949917daf494a1959e Mon Sep 17 00:00:00 2001 From: jcsl94 Date: Wed, 1 Oct 2014 22:58:51 -0300 Subject: [PATCH 4/7] Fixed the median function so that it takes the average if necessary. --- mm.c | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/mm.c b/mm.c index 188d33d..d0b90d6 100644 --- a/mm.c +++ b/mm.c @@ -1,27 +1,38 @@ #include #include +#include #define debug 0 -//Median calculating function -int median(int *pt, int length) -{ - int middle = (length/2); - int median = pt[middle]; - return median; -} - //Mean calculating function double mean(int *pt, int length) { int sum = 0; - double mean = 0; for(int i = 0; i < length; i++) { sum = sum + pt[i]; } - mean = ((double)sum/length); - return mean; + return ((double)sum/length); +} + +//Median calculating function +double median(int *pt, int length) +{ + if(length % 2 == 1) + { + return pt[(length/2)]; + } + else + { + int* midVals; + if((midVals = malloc(2 * sizeof(int))) == NULL) + { + fprintf(stderr, "Could not allocate memory.\n"); + } + midVals[0] = pt[((length/2) - 1)]; + midVals[1] = pt[(length/2)]; + return mean(midVals, 2); + } } // Comparison function for qsort() @@ -73,6 +84,6 @@ int main(int argc, char *argv[]) { fprintf(stdout, "The mean is: %.2f \n", mean(pt, length)); // Call median function - fprintf(stdout, "The median is: %d \n", median(pt, length)); + fprintf(stdout, "The median is: %.2f \n", median(pt, length)); return 0; } From a7e841be59a7e622c5e7b7dfa0505d509f796bce Mon Sep 17 00:00:00 2001 From: jcsl94 Date: Wed, 1 Oct 2014 23:31:15 -0300 Subject: [PATCH 5/7] Got the fork working I think, just need to fix the output. --- mm.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/mm.c b/mm.c index d0b90d6..791a5e7 100644 --- a/mm.c +++ b/mm.c @@ -78,12 +78,26 @@ int main(int argc, char *argv[]) { for (i=0; i 0) + { + wait(NULL); //checks to see if child is finished + //if child is finished, parent calls ad prints mean. + fprintf(stdout, "\nThe mean is: %.2f \n", mean(pt, length)); + } - // Call median function - fprintf(stdout, "The median is: %.2f \n", median(pt, length)); + fprintf(stdout, "\n%s: FIN. \n", argv[0]); return 0; } From 671a6103475a4b5de6405f44c2757164eb89e6ed Mon Sep 17 00:00:00 2001 From: jcsl94 Date: Wed, 1 Oct 2014 23:54:26 -0300 Subject: [PATCH 6/7] Resolved issue #4, resolved issue #5, resolved issue #6, resolved issue #7. Output is mostly fixed other than issue of the sorted numbed being output a second time and I'm not entirely sure as to why that is. --- mm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm.c b/mm.c index 791a5e7..d9ab6c6 100644 --- a/mm.c +++ b/mm.c @@ -98,6 +98,6 @@ int main(int argc, char *argv[]) { fprintf(stdout, "\nThe mean is: %.2f \n", mean(pt, length)); } - fprintf(stdout, "\n%s: FIN. \n", argv[0]); + fprintf(stdout, "%s: FIN. \n\n", argv[0]); return 0; } From 7162610170e9565718cbc0d9f7bb248eef544fef Mon Sep 17 00:00:00 2001 From: jcsl94 Date: Thu, 2 Oct 2014 00:05:09 -0300 Subject: [PATCH 7/7] Fixed the repeated sorted value output, fixed the formatting, also added get pid to the FIN message so it displays the pid as well. --- mm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mm.c b/mm.c index d9ab6c6..4ca3772 100644 --- a/mm.c +++ b/mm.c @@ -79,6 +79,7 @@ int main(int argc, char *argv[]) { fprintf(stdout, "%d ", pt[i]); } + fflush(stdout); // Fork() child calls median, parent calls mean. int rc = fork(); if(rc == -1) @@ -98,6 +99,6 @@ int main(int argc, char *argv[]) { fprintf(stdout, "\nThe mean is: %.2f \n", mean(pt, length)); } - fprintf(stdout, "%s: FIN. \n\n", argv[0]); + fprintf(stdout, "\n%s:(pid:%d) FIN. \n", argv[0], getpid()); return 0; }