Swap Magic
Do you think it is possible to swap values between two variables without using a third temporary variable? What? No? Yes? Well, the answer is...YES. Take a look at the code and don't forget to vote!
AI
Riepilogo AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
Codice sorgente
/*
Swapper
Swaps values between two variables without using a third temporary variable
Programmer: Fazle Arefin
*/
/*
use #include <iostream.h> and cout instead of #include <stdio.h> and printf
if you are using C++, this gives type safety, I didn't use them to keep this
program C compatible
*/
#include <stdio.h>
int main() {
int a = 10;
int b = 20;
printf("Before swapping a = %d and b = %d \n", a, b);
a = a + b;
b = a - b;
a= a - b;
/*
instead of + and - you can use * and / respectively
a = a * b;
b = a / b;
a= a / b;
instead of + and - or * and / you can use the XOR operator ^
but this restricts you to only swapping integers
a = a ^ b;
b = a ^ b;
a= a ^ b;
*/
printf("After swapping a = %d and b = %d \n", a, b);
return 0;
}
Commenti originali (3)
Recuperato da Wayback Machine