// three.cpp -- Finding the smallest of two numbers, sorting three numbers

#include <iostream>
#include <iomanip>

// returns the smallest of two numbers
int min2(int x, int y){ // x and y are passed by value
    if (x > y)
       return y;
    else
       return x;
}

// returns the smallest of three numbers
int min3(int x, int y, int z){ // x, y, z are passed by value
    return min2(x, min2(y,z));
}

// orders two numbers in non-decreasing order
void sort2(int& x, int& y){  // x and y are passed by reference
   if (x > y){
      int temp;
      temp = x;
      x = y;
      y = temp;
   }
}

// orders three numbers in non-decreasing order
void sort3(int& x, int& y, int& z){ // x, y, z are passed by reference
   sort2(x, y);
   sort2(y, z);
   sort2(x, y);
}


void main(void){
   int x1, x2, x3;

   cout << "Enter 3 integers: "; endl;
   cin >> x1 >> x2 >> x3;
   cout << "The smallest of:\n" 
        << "\t" << setw(6) << x1 << endl    //setw(6) sets width of next field
        << "\t" << setw(6) << x2 << endl
        << "\t" << setw(6) << x3 << endl
        << "is\n"
        << "\t" << setw(6) << min3(x1, x2, x3) << endl << endl;

   cout << "Enter 3 integers: "; endl;
   cin >> x1 >> x2 >> x3;
   sort3(x1, x2, x3);
   cout << "The three numbers, in sorted order, are:" << endl 
        << "\t" << setw(6) << x1 << endl
        << "\t" << setw(6) << x2 << endl
        << "\t" << setw(6) << x3 << endl;
}
