[Univ of Cambridge] [Dept of Engineering]
next up previous contents
Next: Calling other programs Up: Examples Previous: Command Line arguments

Using qsort, random numbers and the clock

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
* compile on HPs using c89 -D_HPUX_SOURCE -o filename filename.c */ 

#define NUM 10

int comp(const void *a, const void *b )
    return *(int *)a - * (int *)b;

int main(int argc, char *argv[])
    int numbers[NUM];
    int i;


    printf("\nUnsorted numbers are:-\n");
    for (i=0; i< NUM; i++){
        numbers[i]= 1000 * drand48();
        printf("%d: %3d\n", i, numbers[i]);

    /* See the qsort man page for an explanation of the following */
    qsort((void*) numbers, (size_t) NUM, sizeof(int), comp);

    printf("\nSorted numbers are:-\n");

    for (i=0; i< NUM; i++)
        printf("%d:%3d\n", i, numbers[i]);


Tim Love