new question about sort

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↵
Put your checking and swaping inside loop
1
2
3
4
5
6
7
8
void order( int &a, int &b, int &c, bool ascending ) 
{ 
    if( a < b  ) std::swap(a,b) ; // now b is the smaller of the first two values
    if( b < c  ) std::swap(b,c) ; // now c is the smallest of the three values
    if( a < b  ) std::swap(a,b) ; // now a is the largest of the three values
    
    if(ascending) std::swap(a,c) ; // reverse the order
}


thx, my tried once and type a new code, but it still not right.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void order(int &a, int &b, int &c, bool ascending) 
{ 

	
	if(ascending)
		if (b > c)
			swap(b, c);
		if (a > b)
			swap(a, b);
		if (b > c)
			swap(b, c);
	else
		if (b > c)
			swap(b, c);
		if (a > b)
			swap(a, b);
		if (b > c)
			swap(b, c);
}
i know, it is the sign... damn it
Topic archived. No new replies allowed.