Wrox Programmer Forums A small puzzle
 | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
 C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
 Welcome to the p2p.wrox.com Forums. You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
July 28th, 2006, 06:43 PM
 Registered User Join Date: Jul 2006 Location: , , USA. Posts: 3 Thanks: 0 Thanked 0 Times in 0 Posts
A small puzzle

hai

We have two variables X,Y
and say X = 8;
Y = 5;
Now how can you interchange values of these variables?
with out using third variable
i.e Z = X; X = Y; y = z;
or any function
i.e interchange(X,Y).
This can be achieved by simple mathematical mathod.

http://www.amsterdamhotels.at/
http://www.indianhillresorts.info/
http://www.cheapparishotels.info
July 29th, 2006, 05:13 AM
 Registered User Join Date: Jul 2006 Location: , , Iran. Posts: 6 Thanks: 0 Thanked 0 Times in 0 Posts

hi
u can act like this:

<center>X=X*Y;
Y=X/Y;
X=X/Y;</center>
just that.
August 10th, 2006, 05:54 AM
 Registered User Join Date: Aug 2006 Location: Veliko Tyrnovo, , Bulgaria. Posts: 2 Thanks: 0 Thanked 0 Times in 0 Posts

A little more effective way:

int a = 2;
int b = 3;

a += b; // a = a + b -> a = 5
b = a; // -> b = 2
a -= b; // a = a - b -> a = 3

August 12th, 2006, 03:19 AM
 Registered User Join Date: Jul 2006 Location: , , Iran. Posts: 6 Thanks: 0 Thanked 0 Times in 0 Posts

Y0ur Way Is BEtter & Surely More SEcure & Simple.

I THink S0mewhere In Ur Code THere Is A MIssing (-).
August 17th, 2006, 01:51 PM
 Registered User Join Date: Jul 2006 Location: , , USA. Posts: 3 Thanks: 0 Thanked 0 Times in 0 Posts

Ok

hai iut.aristo you are correct.

hai RoBoT
yours should be like this

a = a + b
b = a - b
a = a - b .

and one long method also

x = e^(x+y)
y = x/e^y
x = x/y
x = ln x
y = ln y ( 5 steps using logarithms )

http://www.amsterdamhotels.at/
http://www.indianhillresorts.info/
http://www.cheapparishotels.info

http://www.amsterdamhotels.at/
http://www.indianhillresorts.info/
http://www.cheapparishotels.info
August 21st, 2006, 02:05 AM
 Registered User Join Date: Jul 2006 Location: , , Iran. Posts: 6 Thanks: 0 Thanked 0 Times in 0 Posts

another interesting way from Ivor Horton's book:

The ^ operator has a surprising property.
Suppose that you have two char variables, "first" with
the value ‘A’, and "last" with the value ‘Z’,
corresponding to binary values "0100 0001" and "0101 1010". If
you write the statements

first ^= last; // Result first is 0001 1011
last ^= first; // Result last is 0100 0001
first ^= last; // Result first is 0101 1010

the result of these is that "first" and "last" have exchanged
values without using any intermediate memory
location.This works with any integer values.
August 22nd, 2006, 06:24 AM
 Registered User Join Date: Aug 2006 Location: , , . Posts: 1 Thanks: 0 Thanked 0 Times in 0 Posts

Hi
Here is the code

a=(a+b)-(b=a);

August 22nd, 2006, 12:20 PM
 Registered User Join Date: Jul 2006 Location: , , Iran. Posts: 6 Thanks: 0 Thanked 0 Times in 0 Posts

and here is another one

a = ( a * b ) / ( b = a );
August 22nd, 2006, 04:11 PM
 Registered User Join Date: Jul 2006 Location: , , Iran. Posts: 6 Thanks: 0 Thanked 0 Times in 0 Posts

and now, another short code fragment:

x = ln ( ( e ^ ( x + y ) ) / ( e ^ ( y = x ) ) )
September 5th, 2006, 12:02 AM
 Registered User Join Date: Aug 2006 Location: Veliko Tyrnovo, , Bulgaria. Posts: 2 Thanks: 0 Thanked 0 Times in 0 Posts

Ups ... working kills the brain, sorry

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is OffTrackbacks are Off Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post puzzle for date ranges navjot C# 1 October 16th, 2008 02:21 PM DataAdapter.Update puzzle wirerider ASP.NET 2.0 Basics 1 August 1st, 2006 06:57 PM Join table puzzle... vanjamier SQL Language 1 November 26th, 2004 04:43 AM Can anyone give me a sample of Crossword Puzzle? sfs00784 General .NET 1 November 6th, 2004 01:37 PM Crossword Puzzle Euler C++ Programming 4 October 19th, 2004 02:08 AM

All times are GMT -4. The time now is 12:38 PM.