// p04_04.cpp: The functions sort4 and sort4p to sort
//             four integers.
#include <iostream>
#include <cstdlib>
using namespace std;

// C++ reference parameters:

void sort2(int &x, int &y)
{  if (x > y) {int t = x; x = y; y = t;}
}

void sort4(int &w, int &x, int &y, int &z)
{  sort2(w, x);  
   sort2(y, z);  // w or y is now the minimum; 
                 // x or z the maximum.
   sort2(w, y);  // w is now the minimum.
   sort2(x, z);  // z is now the maximum.
   sort2(x, y);  // Now w <= x <= y <= z.
}

// C style pointer parameters:

void sort2p(int *p, int *q)
{  if (*p > *q)
   {  int h=*p; *p = *q; *q = h;
   }
}

void sort4p(int *pa, int *pb, int *pc, int *pd)
{  sort2p(pa, pb); 
   sort2p(pc, pd); 
   sort2p(pa, pc); 
   sort2p(pb, pd); 
   sort2p(pb, pc); 
}

int main()
{  int a, b, c, d, a0, b0, c0, d0;
   cout << "Enter four integers: ";
   cin >> a0 >> b0 >> c0 >> d0;
   
   a = a0; b = b0; c = c0; d = d0;
   sort4(a, b, c, d);
   cout << "Sorted by sort4 (reference parameters): " << endl
        << a << " " << b << " " << c << " " << d << endl;
   
   a = a0; b = b0; c = c0; d = d0;
   sort4p(&a, &b, &c, &d);
   cout << "Sorted by sort4p (pointer parameters): " << endl
        << a << " " << b << " " << c << " " << d << endl;
   
   return 0;
}

