Advertisement
cd62131

Use Library's qsort()

Dec 7th, 2013
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. static int comp_double(const void *p1, const void *p2) {
  5.   return *(double *) p1 < *(double *) p2;
  6. }
  7.  
  8. int main(int ac, char **av) {
  9.   printf("How many is random number generated? : ");
  10.   int n = 0;
  11.   scanf("%d", &n);
  12.   double *randoms = (double *) malloc(n * sizeof(double));
  13.   srand((unsigned int) time(NULL));
  14.   for (int i = 0; i < n; i++)
  15.     randoms[i] = (double) rand() / RAND_MAX;
  16.   for (int i = 0; i < n; i++)
  17.     printf("%g, ", randoms[i]);
  18.   puts("");
  19.   qsort(randoms, n, sizeof(double), comp_double);
  20.   printf("1: %g\n2: %g\n", randoms[0], randoms[1]);
  21.   return EXIT_SUCCESS;
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement