Quick Sort with Rational Numbers

  1. Write a function int cmp(const void* p1, const void* p2); for the following main program, so that it can sort an array of rational numbers ascendingly.
    #include <iostream>
    using std::cout;
    using std::endl;
    
    struct Rational {
        int numerator;
        int denominator;
    };
    
    void print_array(Rational a[], int n) {
        for (int i=0; i<n; ++i) {
            cout << a[i].numerator;
            if (a[i].denominator != 1)
                cout << '/' << a[i].denominator;
            if (i != n-1)
                cout << ' ';
            else
                cout << '\n';
        }
    }
    
    int cmp(const void* p1, const void* p2);
    
    int main() {
        Rational A[] = {
            {1, 1}, {1, 2}, {2, 1}, {1, 3}, {3, 1}, {1, 4}, {4, 1} };
        /*   1/1,    1/2,    2/1,    1/3,    3/1,    1/4,    4/1     */
        print_array(A, sizeof(A)/sizeof(A[0]));
        qsort(A, sizeof(A)/sizeof(A[0]), sizeof(A[0]), cmp);
        print_array(A, sizeof(A)/sizeof(A[0]));
        return 0;
    }
  2. You may test your program with another array of rational numbers: { {1,1}, {1,2}, {2,2}, {2,3}, {3,3}, {3,4}, {4,4}, {4,5}, {5,5} }.