q4-qsort.c 580 Bytes
Newer Older
Bruce Cowan's avatar
Bruce Cowan committed
1 2 3 4 5 6 7 8 9 10
#include <stdio.h>
#include <stdlib.h> 

#define N 5

static int
int_compare (const void *a,
             const void *b)
{
    // Dereferencing void pointers requires a cast
Bruce Cowan's avatar
Bruce Cowan committed
11
    return *((const int *) a) - *((const int *) b);
Bruce Cowan's avatar
Bruce Cowan committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
}

int
main (void)
{
    int array[N];

    for (int i = 0; i < N; i++)
    {
        printf ("What is number %d?: ", i+1);
        scanf ("%d", &array[i]);
    }

    qsort (array, N, sizeof (int), int_compare);

    printf ("Sorted array is now:\n");
    for (int i = 0; i < N; i++)
        printf ("%d ", array[i]);
    printf ("\n");
    return 0;
}