Posted By

zaxbbun on 05/21/13


Tagged

swap


Versions (?)

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


 / Published in: C
 

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

  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  

You need to login to post a comment.