From: deo sharma [mailto:[EMAIL PROTECTED] 
>>Paul Herring <[EMAIL PROTECTED]> wrote:
>>
>>From: harry potter [mailto:[EMAIL PROTECTED] 
>>
>>>hi vijay,
>>>
>>>This is the code for swapping:-
>>>
>>>void swap(int &a, int &b)
>>>{
>>>a=a+b;
>>>a=a-b;
>>>b=a-b;
>>>cout<<"\n a is"<<a<<"\n b is"<<b;
>>>}
>>
>>This breaks in two instances:
>>
>>#include <limits.h>
>>int x=y=INT_MAX, z=10;
>>
>>//(1)
>>swap(x, y); // undefined behaviour
>>
>>//(2)
>>swap(z, z); // z==0 after this call.
>
>the answer is quite on expected lines:
> 
>when you add maximum value of an integer with itself again 
>then you are going to get a number which is greater than an 
>integer data type.hence you get an unexpected result.

Indeed.
 
>coming to saecond swap stmt:
>yor are accepting z as refernec so whatever value is there 
>will be reflected back in the main pgm:
> 
>a=10;
>b=10;

Erm since they're pointing to the same variable - try
z = 10;
z = 10;

>a=a+b=10+10=20;

z = z+z = 20;

So far, so good.

>a=a-b=20-10=10;

There *is* no 10 any more.

z = z-z = 0;

This is where it goes wrong.

>b=a-b=10-10=0;

z = z-z = 0;
> 
>henmce we are getting z value as 0

--
PJH

"During my service in the United States Congress, I took the initiative
in creating the Internet." 
    - Al Gore 







Alderley plc, Arnolds Field Estate, The Downs, Wickwar, Gloucestershire, GL12 
8JD, UK
Tel: +44(0)1454 294556 Fax: +44 (0)1454 299272

Website : www.alderley.com  Sales : [EMAIL PROTECTED] Service : [EMAIL 
PROTECTED]

This email and its contents are confidential and are solely for the use of the 
intended recipient. If you are not the original recipient you have received it 
in error and any use, dissemination, forwarding, printing or copying of this 
email is strictly prohibited. Should you receive this email in error please 
immediately notify [EMAIL PROTECTED]

This email has been scanned for viruses, however you should always scan emails 
with your own systems prior to opening.









------------------------ Yahoo! Groups Sponsor --------------------~--> 
$4.98 domain names from Yahoo!. Register anything.
http://us.click.yahoo.com/Q7_YsB/neXJAA/yQLSAA/EbFolB/TM
--------------------------------------------------------------------~-> 

To unsubscribe : [EMAIL PROTECTED]

 
Yahoo! Groups Links

<*> To reply to this message, go to:
    
http://groups.yahoo.com/group/Programmers-Town/post?act=reply&messageNum=4972
    Please do not reply to this message via email. More information here:
    http://help.yahoo.com/help/us/groups/messages/messages-23.html

<*> To visit your group on the web, go to:  
    http://groups.yahoo.com/group/Programmers-Town/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



Reply via email to