From 585bb350fdac026de574bd6f82b47724804713b1 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sun, 24 Oct 2010 14:35:06 -0400 Subject: [PATCH] Add DEBUG and NUM_SHOWN to src/sorting/main.c. --- src/sorting/main.c | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/src/sorting/main.c b/src/sorting/main.c index 7eb791d..824e8c2 100644 --- a/src/sorting/main.c +++ b/src/sorting/main.c @@ -8,15 +8,25 @@ #include "sort.h" + +#define NUM_SHOWN 3 +//#define DEBUG + + void printlist(FILE * stream, int list_size, double *list, int num_shown) { int i; - for (i = 0; i < num_shown; i++) - fprintf(stream, "%f\t", list[i]); - fprintf(stream, "\n"); - for (i = num_shown; i > 0; i--) - fprintf(stream, "%f\t", list[list_size - i]); - fprintf(stream, "\n"); + if (num_shown > 0) { + for (i = 0; i < num_shown; i++) + fprintf(stream, "%g\t", list[i]); + fprintf(stream, "...\n...\t"); + for (i = num_shown; i > 0; i--) + fprintf(stream, "%g\t", list[list_size - i]); + fprintf(stream, "\n"); + } else { + for (i = 0; i < list_size; i++) + fprintf(stream, "%g\n", list[i]); + } } double checklist(int list_size, double *list) @@ -52,7 +62,9 @@ int read_data(const char *file_name, int *pList_size, double **pList) return EXIT_FAILURE; } // read in the data +#ifdef DEBUG fprintf(stderr, "reading %d points\n", *pList_size); +#endif /* DEBUG */ for (i = 0; i < *pList_size; i++) { fscanf(fp, "%lf", &x); (*pList)[i] = x; @@ -77,22 +89,26 @@ int main(int argc, char *argv[]) if (read_data(file_name, &list_size, &list) != EXIT_SUCCESS) return EXIT_FAILURE; +#ifdef DEBUG /* print initial list */ fprintf(stderr, "The list before sorting is:\n"); - printlist(stderr, list_size, list, 3); - fprintf(stderr, "Check: sum of %d elements = %f\n", + printlist(stderr, list_size, list, NUM_SHOWN); + fprintf(stderr, "Check: sum of %d elements = %g\n", list_size, checklist(list_size, list)); +#endif /* DEBUG */ /* sort the list */ sort(list_size, list); +#ifdef DEBUG /* print final list */ fprintf(stderr, "The list after sorting is:\n"); - printlist(stderr, list_size, list, 3); + printlist(stderr, list_size, list, NUM_SHOWN); fprintf(stderr, "Check: sum of %d elements = %g\n", list_size, checklist(list_size, list)); - for (i = 0; i < list_size; i++) - printf("%g\n", list[i]); +#endif /* DEBUG */ + + printlist(stdout, list_size, list, 0); return EXIT_SUCCESS; } -- 2.26.2