Author : James Rogers
Page : << Previous 9
*string;
struct sort_test_t {
char s[16];
} ;
struct sort_test_t sort_test[ELEMENTS];
/* initalize the pseudo-random sequence with the time */
/* if you remove the following command then the program */
/* will generate the same series of "random" numbers each */
/* time the program is ran */
srand(time());
/* inititalize the array */
for (i=0;i<ELEMENTS;i++) {
/* produce a random variable */
x=1+(100000.0*rand()/(RAND_MAX+1.0));
/* load the variable into the string as an array */
sprintf(sort_test[i].s,"%d\000", x);
}
/* sort the array */
qsort(sort_test, ELEMENTS, sizeof(sort_test[0]), &compar);
/* seach the array */
if (string = bsearch("1000", sort_test, ELEMENTS, sizeof(sort_test[0]), &compar)) {
printf("The string 1000 is present.\n");
} else {
printf("The string 1000 is not present!\n");
}
/* output the sorted array */
for (i=0;i<ELEMENTS;i++) {
printf("%d %s\n", i, sort_test[i].s);
}
}
Bibliography:
The ANSI C Programming Language, Second Edition, Brian W. Kernighan, Dennis M. Ritchie, Printice Hall Software Series, 1988
The Standard C Library, P. J. Plauger, Printice Hall P T R, 1992
The Standard C Library, Parts 1, 2, and 3, Chuck Allison, C/C++ Users Journal, January, February, March 1995
STDLIB(3), BSD MANPAGE, Linux Programmer's Manual, 29 November 1993
Page : << Previous 9