swap: swap two numbers without using a third object( or variable)


/ Published in: C
Save to your folder(s)

swap: swap two numbers without using the third object( or variable)


Copy this code and paste it in your HTML
  1. // solution one
  2. //
  3. template <class T>
  4. void swap(T *a, T *b){
  5. *a = *a + *b;
  6. *b = *a - *b;
  7. *a = *a - *b;
  8. }
  9.  
  10.  
  11. // solution two
  12. //
  13. template <class T>
  14. void swap(T &a, T &b){
  15. a = a * b;
  16. b = a / b;
  17. a = a / b;
  18. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.