Advertisement
cd62131

Print Descending Order

Dec 7th, 2013
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.74 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. int compare(const void *p1, const void *p2) {
  6.   return *(int *) p2 - *(int *) p1;
  7. }
  8.  
  9. int main(int ac, char **av) {
  10.   const int DATA_NO = 100;
  11.   int data[DATA_NO];
  12.   int i;
  13.  
  14.   srand((unsigned int) time(NULL));
  15.   data[0] = rand() % 10;
  16.   for (i = 1; i < DATA_NO; i++) {
  17.     data[i] = data[i - 1] + rand() % 10;
  18.     if (data[i] == data[i - 1]) data[i]++;
  19.   }
  20.   for (i = 0; i < DATA_NO; i++) {
  21.     if (i % 10 == 0) printf("\n");
  22.     printf("%4d", data[i]);
  23.   }
  24.   printf("\n");
  25.  
  26.   qsort(data, DATA_NO, sizeof(int), compare);
  27.  
  28.   for (i = 0; i < DATA_NO; i++) {
  29.     if (i % 10 == 0) printf("\n");
  30.     printf("%4d", data[i]);
  31.   }
  32.   printf("\n");
  33.   return EXIT_SUCCESS;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement