/* Example for debugging with GDB, the GNU debugger. Modeled after an example from Norm Matloff, UC Davis http://heather.cs.ucdavis.edu/~matloff/debug.html Warning: this program contains multiple errors! Sorts N integers obtained from standard input. The input consists of the value N, followed by the N items to be sorted. */ #include #define MAXSIZE 10 /* maximum number of items to sort */ /* global variables */ int X[MAXSIZE]; /* input array */ int Y[MAXSIZE]; /* work space */ int numInputs; /* number of items to be sorted */ int numY; /* number of items currently stored in Y */ /* function prototypes */ void getData(); void shift(int j); void insert(int newY); void processData(); void displayResults(); /* function definitions */ int main(void) { getData(); processData(); displayResults(); return 0; } void getData() { int i; /* how many data values? */ scanf("%d", &numInputs); /* read the data and populate the array */ for (i = 0; i < numInputs; i++) scanf("%d", &X[i]); } void shift(int j) { int k; for (k = numY - 1; k > j; k++) Y[k] = Y[k - 1]; } void insert(int newY) { int j; if (numY = 0) { /* insertion into an empty array */ Y[0] = newY; return; } /* insert just before the first Y element that newY is less than */ for (j = 0; j < numY; j++) { if (newY < Y[j]) { /* shift Y[j], Y[j + 1], ... rightward before inserting newY*/ shift(j); Y[j] = newY; return; } } } void processData() { for (numY = 0; numY < numInputs; numY++) /* insert a new item in the proper place among Y[0], ..., Y[numY - 1] */ insert(X[numY]); } void displayResults() { int i; for (i = 0; i < numInputs; i++) printf("%d\n", Y[i]); }