Lost Password?

Go Back   CodeCall Programming Forum > Software Development > Tutorials > C Tutorials

Unregistered, Check out the Coder Battles in the Announcement and Game forums.

C Tutorials All C Tutorials and Code

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-02-2008, 03:39 AM
Chinmoy's Avatar   
Chinmoy Chinmoy is offline
Programming Professional
 
Join Date: Feb 2008
Location: where heaven meets earth
Posts: 301
Credits: 30
Rep Power: 6
Chinmoy has a spectacular aura aboutChinmoy has a spectacular aura about
Default swapping

here is a really simple program to swap 2 numbers without using a third variable.

say you have 2 numbers a and b;
now what you do is add a in b.
then save b-a in a.now save b-a in b.


{
int a=3,b=2;
cout<<"Initially to "<<a<<" "<<b;
{
b+=a;
a=b-a;
b=b-a;
}
cout<<"Swapped to "<<a<<" "<<b;
}

take care of the sequence.b takes a+b. now we put b in a by a=(b-a);meaning a=b(initial value).so now a=b-initial.so b=b-a..
simple.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 03-02-2008, 03:41 AM
Chinmoy's Avatar   
Chinmoy Chinmoy is offline
Programming Professional
 
Join Date: Feb 2008
Location: where heaven meets earth
Posts: 301
Credits: 30
Rep Power: 6
Chinmoy has a spectacular aura aboutChinmoy has a spectacular aura about
Default

try to do this with pointers..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-07-2008, 10:19 PM
broncoslb broncoslb is offline
Learning Programmer
 
Join Date: Feb 2008
Posts: 34
Credits: 0
Rep Power: 3
broncoslb is on a distinguished road
Default Re: swapping

I like it....thinking outside the box
__________________
-Dustin
www.theCprogrammer.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-15-2008, 02:50 PM
dcs dcs is online now
Programmer
 
Join Date: Mar 2008
Posts: 164
Credits: 216
Rep Power: 5
dcs will become famous soon enoughdcs will become famous soon enough
Default Re: swapping

This is not so much of thinking outside the box as it is thinking like we're in the 70's. This is an unoptimization that exhibits undefined behavior.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-15-2008, 03:40 PM
Walle Walle is offline
Newbie
 
Join Date: Mar 2008
Posts: 27
Credits: 0
Rep Power: 3
Walle is on a distinguished road
Default Re: swapping

Quote:
Originally Posted by dcs View Post
This is not so much of thinking outside the box as it is thinking like we're in the 70's. This is an unoptimization that exhibits undefined behavior.
Is it?

What behavior is undefined?

What would you say is a smarter way to do it?
__________________
________________________________________________
"I'm not young enough to know everything." - Oscar Wilde
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 03-15-2008, 03:57 PM
dcs dcs is online now
Programmer
 
Join Date: Mar 2008
Posts: 164
Credits: 216
Rep Power: 5
dcs will become famous soon enoughdcs will become famous soon enough
Default Re: swapping

Quote:
Originally Posted by Walle View Post
Is it?

What behavior is undefined?
When signed arithmetic over/underflows, the behavior is undefined.

Quote:
Originally Posted by Walle View Post
What would you say is a smarter way to do it?
The obvious way, using a temporary.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 03-15-2008, 04:39 PM
Walle Walle is offline
Newbie
 
Join Date: Mar 2008
Posts: 27
Credits: 0
Rep Power: 3
Walle is on a distinguished road
Default Re: swapping

Ah, I get it.

How about unsigned behaviour on overflow? Always wrap or is it also undefined?
__________________
________________________________________________
"I'm not young enough to know everything." - Oscar Wilde
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 03-15-2008, 06:12 PM
dcs dcs is online now
Programmer
 
Join Date: Mar 2008
Posts: 164
Credits: 216
Rep Power: 5
dcs will become famous soon enoughdcs will become famous soon enough
Default Re: swapping

Unsigned integer overflow is well-defined as wrapping.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT -5. The time now is 08:18 PM.

Contest Stats

Xav ........ 1322.18
MeTh0Dz|Reb0rn ........ 1053.7
morefood2001 ........ 879.43
John ........ 877.37
marwex89 ........ 869.98
WingedPanther ........ 830.24
Brandon W ........ 735.07
chili5 ........ 309.39
Steve.L ........ 236.23
dcs ........ 216.02

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 82%

Ads