[ create a new paste ] login | about

Link: http://codepad.org/qgboBPOZ    [ raw code | output | fork ]

C, pasted on Jun 6:
#include <stdio.h>
#include <stdlib.h>

int main()
{
  int n, i, j, flag;
  double *x, t;

  printf("n = ");
  scanf("%d", &n);
  if ((x = (double *)malloc(sizeof(double) * n)) == NULL){
    fprintf(stderr, "cannot allocate enough memory.\n");
    exit(-1);
  }
  for (i = 0; i < n; i++) {
    printf("data %2d: ", i + 1);
    scanf("%lf", &x[i]);
  }

  do {
    flag = 0;    
    for (i = 0; i < n - 1; i++) {
      if (x[i] > x[i + 1]) {
        t = x[i];
        x[i] = x[i + 1];
        x[i + 1] = t;
        flag = 1;
      }
    }
  } while (flag > 0);

  for (i = 0; i < n; i++)
    printf("%lg, ", x[i]);
  putchar('\n');
  free(x);
  return 0;
}
/* end */


Output:
1
2
3
cannot allocate enough memory.
n = 
Exited: ExitFailure 255


Create a new paste based on this one


Comments: