i've just write a code test, but rand() and srand() seem not to be work. Please show me what happens! . thanks a lot.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int cmp(void const* a, void const* b) { return ( *(int*)a - *(int*)b ); }
int main() {
srand( (unsigned int) time(NULL) ); int ar[10],i;
// fill array
for (i = 0; i < 10; ++i)
ar[i] = rand()%20+1; // range (1->20)
// print before sort
for (i = 0; i < 10; ++i)
printf("%d ", i);
// quick sort
qsort( (void*)ar, 10, sizeof(int), cmp );
// print after sort
printf("\n");
for (i = 0; i < 10; ++i)
printf("%d ", i);
return 0;
}
the output not to be changes once i run program, the output is fixed:
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9