qsort(3)

  1. Read the manpage of qsort().
  2. Modify the following code by replacing your own sort_array() with the qsort() provided by standard C library (<cstdlib>).
  3. You need to design a function cmpFunc() so that the following code can sort an array of integers.
    
    // Pointer to Function
    #include <iostream>
    
    int f1(int n) { return -n; }
    int f2(int n) { return (n % 10) + (n / 10); }
    int f3(int n) { return n; }
    
    void sort_array(int a[], int n, int (*f)(int)) {
        int temp;
        for (int i=0; i<n-1; ++i)
            for (int j=i+1; j<n; ++j)
                if (f(a[i]) > f(a[j]))
                {
                    temp = a[i]; a[i] = a[j]; a[j] = temp;
                }
    }
    
    void print_array(int a[], int n) {
        for (int i=0; i<n; ++i)
            std::cout << a[i] << (i==n-1?'\n':' ');
    }
    
    int main()
    {
        int (*pfun[3])(int) = { f1, f2, f3 };
        int a[10] = { 57, 33, 18, 21, 70, 67, 44, 41, 82, 92 };
        for (int i=0; i<3; ++i) {
            sort_array(a, 10, pfun[i]);
            print_array(a, 10);
        }
        return 0;
    }