How to swap two variables, without using third variable ?
Answers were Sorted based on User's Feedback
Answer / harish
only #2 is d right answer......XOR yields...perfect answers.....
| Is This Answer Correct ? | 4 Yes | 1 No |
Answer / balasubramanian ganapthi
a=a+b-(a=b);
This is the example by using only one line to swap the two
variables without using the third variable.
| Is This Answer Correct ? | 4 Yes | 2 No |
Answer / xeon
To note on the answer about Perl, it uses a third variable
and just doesn't tell you. The C++ equivalent is:
//Our data
class exampleclass {};
exampleclass classa, classb;
int inta, intb;
float floata, floatb;
//Our swap function
template<class T>
void swap(T *a, T *b) {
T temp = *a;
a = b;
*b = temp;
}
//Our use of the swap function
swap<classexample>(classa, classb);
swap<int>(inta, intb);
swap<float>(floata, floatb);
And also note, Perl is an interpreted (Script) language, so
though it is simple and robust, it is less efficient than a
language that is compiled.
| Is This Answer Correct ? | 2 Yes | 0 No |
Answer / ankit goel
Sorry i dont know..??
only 1st ans is write......
so give me positive marking....
Thank You...
| Is This Answer Correct ? | 8 Yes | 7 No |
Answer / pallabi
if any one of the variables eithet A or B is negative or
else both r negatibe then how logic will vary in cobol?
| Is This Answer Correct ? | 1 Yes | 0 No |
Answer / sriram
Hey guys, what about swaping variables contains string
values? All of your solutions will suck... Try this and swap
any datatype without third variable... Happy Sensible Coding..
$v = 'sriram';
$u = 'lakshmi';
$v .= $u;
$u = substr($v,0,(strlen($v) - strlen($u)));
$v = substr($v,(strlen($v) - strlen($u)-1), strlen($v));
echo 'u = ' . $u .'<br>';
echo 'v = ' . $v;
| Is This Answer Correct ? | 1 Yes | 0 No |
Is the following code legal? void main() { typedef struct a aType; aType someVariable; struct a { int x; aType *b; }; }
void main() { int i=5; printf("%d",i++ + ++i); }
What is the difference between proc means and proc tabulate ? explain with a simple example when you have to use means or tabulate?
main() { int i, j; scanf("%d %d"+scanf("%d %d", &i, &j)); printf("%d %d", i, j); } a. Runtime error. b. 0, 0 c. Compile error d. the first two values entered by the user
how many processes will gate created execution of -------- fork(); fork(); fork(); -------- Please Explain... Thanks in advance..!
1. const char *a; 2. char* const a; 3. char const *a; -Differentiate the above declarations.
main( ) { int a[ ] = {10,20,30,40,50},j,*p; for(j=0; j<5; j++) { printf(ā%dā ,*a); a++; } p = a; for(j=0; j<5; j++) { printf(ā%d ā ,*p); p++; } }
Can you send Code for Run Length Encoding Of BMP Image in C Language in linux(i.e Compression and Decompression) ?
To reverse an entire text file into another text file.... get d file names in cmd line
hello sir,is there any function in C that can calculate number of digits in an int type variable,suppose:int a=123; 3 digits in a.what ll b answer?
main() { char i=0; for(;i>=0;i++) ; printf("%d\n",i); }
How do I write a program to print proper subset of given string . Eg :input: abc output:{},{a},{b},{c},{a,b},{a,c},{b,c}, {a,b,c}.I desperately need this program please mail me to saravana6m@gmail.com