I am trying to write sort function, sort's job is to rearrange a, b, and c so that a is the smallest #, c is the largest #.
I wrote one and it worked but it looks so long and I want to know a short way
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
void sort(double & a, double & b, double & c)
{
if (a >= b && b >= c)
cout <<"Smallest to biggest: "<< c << " " << b << " " << a <<endl;
elseif (a >= c && c >= b)
cout <<"Smallest to biggest: "<< b << " " << c << " " << a << endl;
elseif (b >= a && a >= c)
cout <<"Smallest to biggest: "<< c << " " << a << " " << b << endl;
elseif (b >= c && c >= a)
cout <<"Smallest to biggest: "<< a << " " << c << " " << b << endl;
elseif (c >= a && a >= b)
cout <<"Smallest to biggest: "<< b << " " << a << " " << c << endl;
else
cout <<"Smallest to biggest: "<< a << " " << b << " " << c << endl;
}
void mySwap(int &first, int &second)
{
int t = second;
second = first;
first = t;
}
void mySort(int &a, int &b, int &c)
{
if(a>b)
mySwap(a,b);
if(a>c)
mySwap(a,c);
if(b>c)
mySwap(b,c);
}