#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)
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;
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;
}