swapping program does not use third variable
Answers were Sorted based on User's Feedback
Answer / jayabalan.s
void main()
{
int a,b;//example a=10,b=12;
a=a+b;//a=22
b=a-b;//b=10;
a=a-b;//a=12
}
Is This Answer Correct ? | 17 Yes | 0 No |
Answer / jayasrinivas.donavalli
class swap
{
public:
int a,b;
void swapp()
{
a = a + b;
b = a - b;
a = a - b;
}
};
class swap s1.
s1.swapp();
cout<<a<<b;
Is This Answer Correct ? | 9 Yes | 3 No |
Answer / venkanna
swap(a,b);
{
a=a+b;
b=a-b;
a=a-b;
}
Explenation:
if a=5 and b=10
a=a+b(5+10)=15
b=a-b(15-10)=5------------swaped here
a=a-b(15-5)=10------------
Is This Answer Correct ? | 6 Yes | 2 No |
Answer / abdul rahman
void main()
{
int a,b;//example a=10,b=12;
a=a~b;//a=22
b=a~b;//b=10;
a=a~b;//a=12
}
NOTE:
~=XOR operator( not able to write xor
operator so iam using this symbol which is not
correct)
Is This Answer Correct ? | 4 Yes | 2 No |
Answer / rajeshwar raja
Swap(int* a, int* b)
{
(*a) ^= (*b) ^= (*a) ^= (*b);
}
Explanation:
Expressions are evaluated from right to left.
Take the right most operation (*a) ^= (*b), its is short
hand XOR operation between 'a' and 'b'.
Assume a = 2 and b = 3.
a = 2 ^ 3 = 1
Next operation (*b) ^= (*a),
b = 3 ^ 1 = 2 (Note a is 1 now) ***(b is 2)***
Next operation (*a) ^= (*b)
a = 1 ^ 2 = 3 (Note b is 2 now) ***(a is 3)***
SWAPPED!!!
It can swap complex data structures also.
Is This Answer Correct ? | 1 Yes | 0 No |
What is polymorphism what is it for and how is it used?
What is the difference between pass by value,pass by pointer,pass by reference in the catch block in the exception handling in c++
What are the different forms of polymorphism??
What is a class in oop?
Explain virtual inheritance?
suppose A is a base class and B is the derved class. Both have a method foo which is defined as a virtual method in the base class. You have a pointer of classs B and you typecast it to A. Now when you call pointer->foo, which method gets called? The next part of the question is, how does the compiler know which method to call?
How can you overcome the diamond problem in inheritance?
Explain the advantages of inheritance.
What is multilevel inheritance explain with example?
Describe the difference between a Thread and a Process?
What is polymorphism programming?
What is multilevel inheritance in oop?