From f154c8e996541ed22f3e27c1b3def3953b6ed417 Mon Sep 17 00:00:00 2001 From: JieZhang0918 Date: Thu, 25 Sep 2014 16:32:52 -0300 Subject: [PATCH 1/2] create a mm.c --- 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, 2 Oct 2014 00:52:38 -0300 Subject: [PATCH 2/2] assighment1 --- mm.c | 158 +++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 106 insertions(+), 52 deletions(-) diff --git a/mm.c b/mm.c index ff1779f..8e1a0fd 100644 --- a/mm.c +++ b/mm.c @@ -1,52 +1,106 @@ -#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 +#include +#include +#include + +#define debug 0 + +//caculate the mean value +int mean(int* ptr, int length) +{ + int mean=0;//mean value + int i; + for(i=0; i y) return 1; + if (x < y) return -1; + return 0; +} + +int main(int argc, char *argv[]) { + + int i, length, *pt; + int getmean, getmedian;// for mean and median + // 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