Define a function
void order(int & a, int & b, int & c, bool ascending)
that places the values in ascending or descending order, depending on whether the last parameter is true or false . For example, if x, y and z contain the values 3,1, and 2, respectively, then after calling order(x,y,z, true ) the same variables will contain values 1, 2 and 3, respectively. On the other hand a call to order(x,y,z, false ) will cause the values to be 3,2 and 1, respectively. You may use the built in C++ function swap() if you wish.
Note that the driver will use four values for input: the three numbers to be passed into a. b, and c, followed by a 0 or a 1 (i.e. false or true ) to pass into ascending (the fourth parameter ).
Here is my solution:
void order(int &a, int &b, int &c, bool ascending)
{
int temp;
if(ascending)
{
if(a < b)
{
swap(a,b);
}
else if(b < c)
{
swap(b,c);
}
else
{
swap(a,c);
}
}
else
{
temp = a;
a = b;
b = c;
c = temp;
}
}
Here is my output:
2 1 3
Expected output:
1 2 3
I don't understand what I'm doing wrong. Is there something that needs to be added or changed in the function. I would greatly appreciate any help.