[ create a new paste ] login | about

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

C, pasted on Sep 28:
#include <stdio.h>
#include <stdlib.h>

void bubble_sort(int* num, int size) {
  int i, j, temp;
  for (i = 0; i < size - 1; i++)
    for (j = 0; j < size - 1; j++) {
      if (num[j + 1] < num[j]) {
        temp = num[j]; num[j] = num[j + 1]; num[j + 1] = temp;
      }
    }
}

#define FILENAME "sample.txt"
#define MAX 1000
#define N 1024
int main() {
  static char buff[N];
  int table[MAX];
  int i, size;
  FILE *fp;
  if ((fp = fopen(FILENAME, "r")) == NULL) {
    fprintf(stderr, "cannot open\n");
    return -1;
  }
  i = 0;
  while (fgets(buff, N, fp) != NULL) {
    table[i++] = atoi(buff);
    if (i > MAX)
      break;
  }
  size = i;
  bubble_sort(table, size);
  for (i = 0; i < size; i++)
    printf("%d:%d\n", i, table[i]);
  return 0;
}
/* end */


Output:
1
2
3
cannot open

Exited: ExitFailure 255


Create a new paste based on this one


Comments: