Search Contact information
University of Cambridge Home Department of Engineering
University of Cambridge >  Engineering Department >  computing help
next up previous contents
Next: Calling other programs Up: Examples Previous: Command Line arguments   Contents

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;

    srand48((long)time(NULL));

    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]);

    exit(0);
}



Tim Love 2010-04-27