Sort an array of rational numbers

  1. Modify your own exchange sort function to sort an array of rational numbers.
  2. Suppose you defined
    1. a structure
      
      struct Rational {
          int q;	// Numerator
          int p;	// Denominator
          };
      
      
    2. a function to display the array
      
      void print_array(Rational a[], int n)
      {
          for (int i=0; i<n; i++)
              cout << a[i].q << '/' << a[i].p << ' ';
          cout << endl;
      }
      
      
    3. and a main program to test it.
      
      int main()
      {
          const int N = 4;
          Rational a[N];
          cout << "Please input 4 rational numbers" << endl;
          for (int i=0; i<N; i++)
          {
              cin >> a[i].q >> a[i].p;
          }
          cout << endl;
          print_array(a, N);
          exchange_sort(a, N, cmp);
          print_array(a, N);
          return 0;
      }
      
      
  3. The program may run as follows:
    
    Please input 4 rational numbers
    7 3
    1 5
    6 5
    4 3
    
    7/3 1/5 6/5 4/3
    1/5 6/5 4/3 7/3
    
    
  4. Now all you need to supply is a cmp() function and a revised exchange_sort() function.