Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old July 28th, 2006, 06:43 PM
Registered User
 
Join Date: Jul 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
Reply With Quote
  #2 (permalink)  
Old July 29th, 2006, 05:13 AM
Registered User
 
Join Date: Jul 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi
u can act like this:

<center>X=X*Y;
Y=X/Y;
X=X/Y;</center>
just that.
Reply With Quote
  #3 (permalink)  
Old August 10th, 2006, 05:54 AM
Registered User
 
Join Date: Aug 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to RoBoT
Default

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

Reply With Quote
  #4 (permalink)  
Old August 12th, 2006, 03:19 AM
Registered User
 
Join Date: Jul 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

THanks For Y0ur reply RoBoT.

Y0ur Way Is BEtter & Surely More SEcure & Simple.

I THink S0mewhere In Ur Code THere Is A MIssing (-).
Reply With Quote
  #5 (permalink)  
Old August 17th, 2006, 01:51 PM
Registered User
 
Join Date: Jul 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote
  #6 (permalink)  
Old August 21st, 2006, 02:05 AM
Registered User
 
Join Date: Jul 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
Reply With Quote
  #7 (permalink)  
Old August 22nd, 2006, 06:24 AM
Registered User
 
Join Date: Aug 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to muralidhar.allam
Default

Hi
Here is the code

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

Reply With Quote
  #8 (permalink)  
Old August 22nd, 2006, 12:20 PM
Registered User
 
Join Date: Jul 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

and here is another one


a = ( a * b ) / ( b = a );
Reply With Quote
  #9 (permalink)  
Old August 22nd, 2006, 04:11 PM
Registered User
 
Join Date: Jul 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

and now, another short code fragment:

    x = ln ( ( e ^ ( x + y ) ) / ( e ^ ( y = x ) ) )
Reply With Quote
  #10 (permalink)  
Old September 5th, 2006, 12:02 AM
Registered User
 
Join Date: Aug 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to RoBoT
Default

Ups ... working kills the brain, sorry

Reply With Quote





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.