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 ).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
void order(int &a, int &b, int &c, bool ascending)
{
int temp;
if(ascending)
{
if (a < b) {
if (c < a)
swap(a,c);
}
else {
if (b < c)
swap(a,b);
else
swap(a,c);
}
if(c<b)
swap(b,c);
}
else
{
temp = a;
a = b;
b = c;
c = temp;
}
}
|
Expected Output:
3·2·1↵
Actual Output:
231↵ |