Velocity Reviews > C++ > Re: Sort 3 numbers in a single line.

# Re: Sort 3 numbers in a single line.

Hasan Ammar
Guest
Posts: n/a

 09-16-2004
This was a Puzzle question presented to us to determine if it could be
done in a single return statement. The sorting should be done in such a
way that your return value becomes (a < b) < c hence the result is a
boolean value.

chris
Guest
Posts: n/a

 09-16-2004
Hasan Ammar wrote:

> This was a Puzzle question presented to us to determine if it could be
> done in a single return statement. The sorting should be done in such a
> way that your return value becomes (a < b) < c hence the result is a
> boolean value.
>

I'm still not positive what you mean. However I can tell you in theory
you can do anything at all in one return statement

The following are your friends:

the ?: operator, often used normally.

the , operator: not used that often normally, it acts most like a
semi-colon, but only the last part of it is returned:

Remember with a && b, the b part is only run if a was true, and for a ||
b, b is only run if a is false.

To start you off, here is a little example:

#include <stdio.h>
int swap(int &a,int &b,int &c){
return (
((a>b)&&(a+=b,b-=a,b=-b,a-=b)),((b>c)&&(b+=c,c-=b,c=-c,b-=c)),((a>b)&&(a+=b,b-=a,b=-b,a-=b)));
}

int main(void) {
int a=3,b=2,c=1;
swap(a,b,c);
printf("%d,%d,%d",a,b,c);
}

This program actually ignores the return value of swap, and pass a,b and
c by reference. It gives you the idea of what to do tho.

Note that (a+=b,b-=a,b=-b,a-=b) is one way of swapping the values of a
and b (not a very good way actually, as it breaks if the sum overflows
somewhere in the middle, but to be honest this whole idea is quite stupid).

As you can probably guess, using , && and || it is actually possible to
write entire programs which don't appear to actually have any "normal"
instructions in them. Writing one or two is quite fun, although you
shouldn't show such code to other people, except to confuse them.

Chris

steve
Guest
Posts: n/a

 09-16-2004
Hi,

Just a note, since you are being given puzzle's I assume that you are
learning to code ( University I guess ). Enjoy the puzzle and the
interesting new functionality that it introduces you to ( i.e. ? : , ^ ).

BUT..

Doing stuff like this has got very VERY little to do with developing
software, and more to do with developing egos.
It would be a very bad habit to get into if you started to look for way to
"shorten" code. "Short" code is not fast code.
The code below while it may work is nothing short of poor code ( as Chris
suggested "...shouldn't show such code to other people" )

-Steve

"chris" <(E-Mail Removed)> wrote in message
news:cic74n\$bjl\$(E-Mail Removed)...
> Hasan Ammar wrote:
>
> > This was a Puzzle question presented to us to determine if it could be
> > done in a single return statement. The sorting should be done in such a
> > way that your return value becomes (a < b) < c hence the result is a
> > boolean value.
> >

>
> I'm still not positive what you mean. However I can tell you in theory
> you can do anything at all in one return statement
>
> The following are your friends:
>
> the ?: operator, often used normally.
>
> the , operator: not used that often normally, it acts most like a
> semi-colon, but only the last part of it is returned:
>
> Remember with a && b, the b part is only run if a was true, and for a ||
> b, b is only run if a is false.
>
> To start you off, here is a little example:
>
> #include <stdio.h>
> int swap(int &a,int &b,int &c){
> return (
>

((a>b)&&(a+=b,b-=a,b=-b,a-=b)),((b>c)&&(b+=c,c-=b,c=-c,b-=c)),((a>b)&&(a+=b,
b-=a,b=-b,a-=b)));
> }
>
> int main(void) {
> int a=3,b=2,c=1;
> swap(a,b,c);
> printf("%d,%d,%d",a,b,c);
> }
>
> This program actually ignores the return value of swap, and pass a,b and
> c by reference. It gives you the idea of what to do tho.
>
> Note that (a+=b,b-=a,b=-b,a-=b) is one way of swapping the values of a
> and b (not a very good way actually, as it breaks if the sum overflows
> somewhere in the middle, but to be honest this whole idea is quite

stupid).
>
> As you can probably guess, using , && and || it is actually possible to
> write entire programs which don't appear to actually have any "normal"
> instructions in them. Writing one or two is quite fun, although you
> shouldn't show such code to other people, except to confuse them.
>
> Chris

chris
Guest
Posts: n/a

 09-16-2004
steve wrote:

> Hi,
>
> Just a note, since you are being given puzzle's I assume that you are
> learning to code ( University I guess ). Enjoy the puzzle and the
> interesting new functionality that it introduces you to ( i.e. ? : , ^ ).
>
> BUT..
>
> Doing stuff like this has got very VERY little to do with developing
> software, and more to do with developing egos.
> It would be a very bad habit to get into if you started to look for way to
> "shorten" code. "Short" code is not fast code.
> The code below while it may work is nothing short of poor code ( as Chris
> suggested "...shouldn't show such code to other people" )
>

Yes, I should probably have said this steve thanks!

One of the most important things is that in c++, if you want to swap two
integers a and b, then {int temp=a; a=b; b=temp;} is the FASTEST way of
doing it. It's faster than stupid XORing tricks. It's faster than stupid
addition / subtraction tricks. Worry about the algorithms and let your
comppiler worry about bit-twiddling (OK there are exceptions like there
are to everything, but in general)

Method Man
Guest
Posts: n/a

 09-16-2004

"Hasan Ammar" <(E-Mail Removed)> wrote in message
news:cic5vj\$(E-Mail Removed)...
> This was a Puzzle question presented to us to determine if it could be
> done in a single return statement. The sorting should be done in such a
> way that your return value becomes (a < b) < c hence the result is a
> boolean value.
>

You're still not making the problem clear. Do you want the function to
return true if a < b < c ? If so:

bool sort(int a, int b, int c) {
return ((a < b) ? (b < c) : 0);
}

Or do you want the single statement to perform a sort? In which case, why
would your function return a 'bool'?

Method Man
Guest
Posts: n/a

 09-16-2004
- snip -

> Note that (a+=b,b-=a,b=-b,a-=b) is one way of swapping the values of a
> and b (not a very good way actually, as it breaks if the sum overflows
> somewhere in the middle, but to be honest this whole idea is quite

stupid).

Since we're trying to shorten code, why not implement that swap in 3
statements instead of 4?

a = a + b;
b = a - b;
a = a - b;

Karl Heinz Buchegger
Guest
Posts: n/a

 09-17-2004
Method Man wrote:
>
> - snip -
>
> > Note that (a+=b,b-=a,b=-b,a-=b) is one way of swapping the values of a
> > and b (not a very good way actually, as it breaks if the sum overflows
> > somewhere in the middle, but to be honest this whole idea is quite

> stupid).
>
> Since we're trying to shorten code, why not implement that swap in 3
> statements instead of 4?
>
> a = a + b;
> b = a - b;
> a = a - b;

Because it is still a bad way of swapping

--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)

JKop
Guest
Posts: n/a

 09-17-2004

> Note that (a+=b,b-=a,b=-b,a-=b) is one way of swapping the values of a
> and b (not a very good way actually, as it breaks if the sum overflows
> somewhere in the middle, but to be honest this whole idea is quite
> stupid).

BULLSHIT

Try this on an 8-Bit char system, like Windows.

The range of unsigned char will be 0 to 255.

unsigned char a = 250;
unsigned char b = 100;

The swap will still work. Why? Here's a clue: defined over-flow.

-JKop

Karl Heinz Buchegger
Guest
Posts: n/a

 09-17-2004
JKop wrote:
>
> > Note that (a+=b,b-=a,b=-b,a-=b) is one way of swapping the values of a
> > and b (not a very good way actually, as it breaks if the sum overflows
> > somewhere in the middle, but to be honest this whole idea is quite
> > stupid).

>
> BULLSHIT
>
> Try this on an 8-Bit char system, like Windows.
>
> The range of unsigned char will be 0 to 255.
>
> unsigned char a = 250;
> unsigned char b = 100;
>
> The swap will still work. Why? Here's a clue: defined over-flow.
>

Now try the same with double values.
Good luck.

Note: When talking about a swap function, most programmers
think about a function (or as in C++: a tamplate) that works
with each and every data type. Granted, there are special cases where
all those swap_hacks work, but in practice this is completely
unimportant, since the 3 assignment sequence paired with a temporary
works well enough in 99.999999% of all cases.

--
Karl Heinz Buchegger
(E-Mail Removed)

Andre Heinen
Guest
Posts: n/a

 09-17-2004
On Fri, 17 Sep 2004 12:13:49 GMT, JKop <(E-Mail Removed)> wrote:

>BULLSHIT
>
>Try this on an 8-Bit char system, like Windows.
>
>The range of unsigned char will be 0 to 255.
>
>unsigned char a = 250;
>unsigned char b = 100;
>
>The swap will still work. Why? Here's a clue: defined over-flow.

The example Chris gave uses int's. In signed arithmetic,
overflow is undefined behaviour.

--
Andre Heinen
My address, rot13-encoded: n qbg urvara ng rhebcrnayvax qbg pbz