Sort 3 numbers in a single line.

H

Hasan Ammar

Lets suppose we have a function to sort 3 integers, is it possible to
sort and return them in a single statement?

example:

bool sort(int a, int b, int c) {
return // this is where the code goes.... ;
}

is it possible without using an external function to swap two values?
 
G

Gernot Frisch

Hasan Ammar said:
Lets suppose we have a function to sort 3 integers, is it possible
to
sort and return them in a single statement?

example:

bool sort(int a, int b, int c) {
return // this is where the code goes.... ;
}

na = max(a, max(b,c));
nc = min(a, min(b,c));
nb = (na==a)?(nc==c ? b : c) : (nc==c ? b : a);
is it possible without using an external function to swap two
values?

for integers only:
#define swap(a,b) {(a)=(b)^(a); (b)=(a)^(b); (a)=(b)^(a);}


--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
 
C

chris

Hasan said:
Lets suppose we have a function to sort 3 integers, is it possible to
sort and return them in a single statement?

example:

bool sort(int a, int b, int c) {
return // this is where the code goes.... ;
}

I think you need to think a bit more about what you want to do..

for a start, your function returns bool. You can't return a list of 3
sorted integers via a bool.

Do you want something like void sort(int &a,int &b,int &c) {..}, where
sort(x,y,z) will change the actual values of x,y and z, or do you want
something more like vector<int> sort(int a,int b,int c), where you
return a vector with the variables sorted?

is it possible without using an external function to swap two values?

Do you want to do this for any particular reason? Don't bother for
efficency reasons, as any decent compiler will optimise away a call to a
swapping function if the swapping function is sufficently small.
 
J

JKop

for integers only:
#define swap(a,b) {(a)=(b)^(a); (b)=(a)^(b); (a)=(b)^
(a);}

I know this argument has been done to death but I'm going
to say it anyway:

swap(++i,k+=2);


....and that's just because I've declared war on macros.

ALL MACROS WILL SUFFER AND DIE A VERY PAINFUL DEATH, I WILL
SEE TO IT.

-JKop
 
G

Gernot Frisch

JKop said:
(a);}

I know this argument has been done to death but I'm going
to say it anyway:

swap(++i,k+=2);


...and that's just because I've declared war on macros.

ALL MACROS WILL SUFFER AND DIE A VERY PAINFUL DEATH, I WILL
SEE TO IT.

Ah, c'mon. It's faster to write one that doing a function, but OK,
you're totally right.

template<class T> inline void swap(T&1, T&2)
{
(a)=(b)^(a); (b)=(a)^(b); (a)=(b)^(a);
}
 
R

Richard Herring

[QUOTE="JKop said:
for integers only:
#define swap(a,b) {(a)=(b)^(a); (b)=(a)^(b); (a)=(b)^
(a);}

I know this argument has been done to death but I'm going
to say it anyway:

swap(++i,k+=2);[/QUOTE]

swap(a, a);
 
R

Richard Herring

Gernot Frisch said:
Ah, c'mon. It's faster to write one that doing a function, but OK,
you're totally right.

template<class T> inline void swap(T&1, T&2)
{
(a)=(b)^(a); (b)=(a)^(b); (a)=(b)^(a);
}

swap(a, a);
 
K

Karl Heinz Buchegger

Gernot said:
Ah, c'mon. It's faster to write one that doing a function, but OK,
you're totally right.

template<class T> inline void swap(T&1, T&2)
{
(a)=(b)^(a); (b)=(a)^(b); (a)=(b)^(a);
}

int i = 5;
swap( i, i );

-> i has a value of 0
 
I

Ioannis Vranos

Hasan said:
Lets suppose we have a function to sort 3 integers, is it possible to
sort and return them in a single statement?

example:

bool sort(int a, int b, int c) {
return // this is where the code goes.... ;
}

is it possible without using an external function to swap two values?


What does it suppose to mean to "sort three integers, int a, int b, int c"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top