C/C++ simple challenge

Status
Not open for further replies.

shoegoe

Broken In

we all know how to swap numbers in a program..

BUT we use a variable temp/like that to store the initial value..

can the same be done without using a TEMP variable?
 

cg84

Broken In
very simple...
take 2 nos. x & y and do the following:
x=x+y
y=x-y
x=x-y
and ur nos r swapped... :)
 

cooljeba

The Photoshop Guy
cg84 said:
very simple...
take 2 nos. x & y and do the following:
x=x+y
y=x-y
x=x-y
and ur nos r swapped... :)
this is the best way to do it with out using a temp variable ;)


..:: peace ::..
jeba
 

cnukutti

Broken In
What is the thing about manipulating functions??
and what do you mean by printing itself.
should it read the .c file and print the source code???
 

DKant

In the zone
NikhilVerma said:
Ok now here is another simple one....

Do the same swapping with strings... without using temp variable...

Simple? Well I dunno. Sumthin' with pointers? But I don't see how u can do any manips on pointers. :?

Apart from that, swapping without temps isn't fool-proof. For example, if ur dealing with 2 integers of the order of 32767/65535 (basically sumwhere close to the max.) , then u cld run into problems.
 

alib_i

Cyborg Agent
@swatcat ...
frankly speaking .. even i dont know ... this exactly was a problem statement in one programming contest long time back ..
i still dont know wht it means and how to solve it
 
OP
shoegoe

shoegoe

Broken In
cg84 said:
very simple...
take 2 nos. x & y and do the following:
x=x+y
y=x-y
x=x-y
and ur nos r swapped... :)

Ths...
BUT

How to swap more than 2 variables .....
(DONT TELL ME TO USE THE SAME CODE FOR EACH 2 SWAPS

ANSWERS??
 
Status
Not open for further replies.
Top Bottom