473,385 Members | 1,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Swapping in C++

16
Write C++ code to replace two variables A and B with each other.
Given A=2, B=1, you need to swap the values of A and B without using any third variable.


Hints
Assign the value stored in A to the variable B and the value stored in B to the variable A without using any third variable.
Oct 16 '06 #1
2 2198
Banfa
9,065 Expert Mod 8TB
What a useless hint, it is exactly the same as the actual question.

A better hint is that the best solution (I have seen) uses the xor operator ^

consider what A ^ B ^ B is
Oct 16 '06 #2
vermarajeev
180 100+
Expand|Select|Wrap|Line Numbers
  1. int main(int argc, char** argv[])
  2. {
  3.     int a,b;
  4.     cout<<"Enter two numbers"<<endl;
  5.     cin>>a>>b;
  6.  
  7.     cout<<"Before swapping"<<endl;
  8.     cout<<"a:"<<a<<"\t"<<"b:"<<b<<endl;
  9.  
  10.     //solution1
  11.     a = a^b;
  12.     b = b^a;
  13.     a = a^b;
  14.  
  15.     //solution2
  16.     /*a = a+b;
  17.     b = a-b;
  18.     a = a-b;*/
  19.  
  20.     cout<<"After swapping"<<endl;
  21.     cout<<"a:"<<a<<"\t"<<"b:"<<b<<endl;
  22.  
  23.     return 0;
  24. }
Oct 17 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Christopher Jeris | last post by:
Please help me understand the differences, in semantics, browser support and moral preferredness, between the following three methods of swapping content in and out of a page via JavaScript. I...
16
by: Sethu Madhavan | last post by:
Hi All Here i am facing one technical issue related to Swapping the Object Instance in Dotnet. e.g.. class Customer{ Order _lastOrder;
4
by: alanrn | last post by:
I am using a TreeView to display the hierarchy of a strongly-typed collection (inherited from CollectionBase). The order of the nodes in the TreeView is strictly tied to the order in which they...
270
by: Jatinder | last post by:
I found these questions on a web site and wish to share with all of u out there,Can SomeOne Solve these Porgramming puzzles. Programming Puzzles Some companies certainly ask for these...
28
by: rajendra.stalekar | last post by:
Hi Folks!!! I have a string let's say "hi" and got to reverse it using just a single variable for swapping, how do I do it? Regards, Rajendra S.
2
by: Sam Marrocco | last post by:
Does anyone know of a method of swapping collection items? For example: Current collection: ..Item(1)="a" ..Item(2)="b" ..Item(3)="c" After swapping items 2 and 1.... ..Item(1)="b"...
34
by: Ann | last post by:
I am opening a file which looks like 0xABCDEF01 on another machine but 0x01EFCDAB on my machine. Is this a byte swapping? Could anyone give a good way to check if bytes are being swapped?...
2
by: Protoman | last post by:
How would I write a user defnable letter swapping algorithm? I've written an Enigma encrypting program, and I need to redo the plugboard function. Right now, the letter swapping is fixed. I need to...
1
by: subramanian100in | last post by:
Will the following code always work ? #include <stdio.h> #include <stdlib.h> # define SIZE 50 void myswap(char **name1, char **name2); int main(void)
5
by: arnuld | last post by:
it works fine: /* C++ Primer - 4/e * * example from section 7.2.2, pointer-swap * STATEMENT * in a function call where parameters are pointers, we actually copy the pointers. * here in this...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.