Program Logic Steps:
C++ code to swap two variables without using third variable:
#include<iostream>
using namespace std;
int main()
{
int var1, var2;
cout<<"Enter value for first integer: ";
cin>>var1;
cout<<"Enter value for second integer: ";
cin>>var2;
cout<<" Values Before swapping: "<<endl;
cout<<"First Integer ="<<var1<<endl;
cout<<"Second Interger ="<<var2<<endl;
var1=var1+var2;
var2=var1-var2;
var1=var1-var2;
cout<<" Values After swapping: "<<endl;
cout<<"First Integer ="<<var1<<endl;
cout<<"Second Interger ="<<var2<<endl;
return 0;
}
Sample Output
Image view of code:
- Store sum of both in var1.
- Subtract var2 from var1 and store it in var2 (now var2 has value of var1) .
- Subtract var2 from var1 and store it in var1 (now var1 has value of var2).
C++ code to swap two variables without using third variable:
#include<iostream>
using namespace std;
int main()
{
int var1, var2;
cout<<"Enter value for first integer: ";
cin>>var1;
cout<<"Enter value for second integer: ";
cin>>var2;
cout<<" Values Before swapping: "<<endl;
cout<<"First Integer ="<<var1<<endl;
cout<<"Second Interger ="<<var2<<endl;
var1=var1+var2;
var2=var1-var2;
var1=var1-var2;
cout<<" Values After swapping: "<<endl;
cout<<"First Integer ="<<var1<<endl;
cout<<"Second Interger ="<<var2<<endl;
return 0;
}
Sample Output
Image view of code:
Interesting exercise. Just to make sure people aren't led astray, I'd like to point out why it should never, ever be done in a "real" program:
ReplyDelete1. It will only work with reasonably small integers. If you get an overflow (i.e., var1+var2 >= 2^31), the result is undefined.
2. It won't work for float or double, because floating point addition and subtraction is not perfectly accurate.
3. It won't work for other types.
4. It is hard to read.
5. It is inefficient.
Instead:
using std::swap;
swap(var1, var2);
You Are Absolutely Right
DeleteBut This Program IS For Absolute Beginners That Is Why Program Coded In Very Simple Format With Simple Logic So Anyone Can Get The Basic Idea Of Swapping.
Your Points Are Valid For Advanced Level.
Thanks A Lot.. For Giving Your Time.
I was going to suggest the xor trick (for integer types), because it doesn't have the overflow problem, but after some thought and reading of the spec, I realized:
ReplyDelete1. For unsigned integer types, overflow is impossible (because the C++ standard mandates that unsigned arithmetic be done modulo 2**n), so both the addition-subtraction method, and the xor method, will work.
2. For signed types, with some representations it might be possible for the xor method to create invalid values (for example, in signed magnitude or 1s complement representations, depending on how positive and negative zeros are treated) resulting in undefined behavior. With 2s complement representation, both the addition-subtraction method and the xor method will probably work (because the easiest way to implement 2s complement arithmetic is to have it wrap around).
Anyway, anonymous is right, std::swap is always the right answer!
I want a program to search an element in an array and replace every occurrence with 0.
ReplyDeleteExample: Input is 1232472. The places where 2 is present must be made 0 so that we will get 1030470 and it must be swapped as 1347000. How to write a program for that?
#include
Deleteusing namespace std;
void main(void)
{
int x, i,j;
int arr[] = { 1, 2, 3, 2, 4, 7, 2 };
for (i = 0; i < 7; i++)
{
if (arr[i] == 2) arr[i] = 0;
}
for (i = 0; i < 7; i++)
cout << arr[i];
cout << endl;
for (i = 0; i < 7; i++)
{
if (arr[i] == 0)
{
for (j = i; j < 7; j++){
if (arr[j] =! 0){
arr[i] = arr[j];
arr[j] = 0;
break;
}
}
}
}
for (i = 0; i < 7; i++)
cout << arr[i];
system("pause");
}
its not working but you get the idea!!!!!!!!!!!!
Thanks for the program.
ReplyDeleteA very good article. Thanks a lot
ReplyDeleteThanks for such a nice article .
ReplyDeleteswapping of two numbers
Thanks a lot fr d effort sir can i hav more n more exercise on basis of data structure ?
ReplyDeleteWould this work for long datatype?
ReplyDeleteWould this program work for long datatype?
ReplyDelete