swap in place

/************************************************************************/
/* 17.1 Write a function to swap a number in place without temporary variables.    */
/*     a, b;
       a+b, b;
       a+b, a;
       b, a;
/************************************************************************/

#include<iostream>

using namespace std;

void swapinplace(int *a, int *b) {
       *a = *a + *b;
       *b = *a - *b;
       *a = *a - *b;

}

Comments

Popular posts from this blog

Sudoku

Longest prefix matching - trie

Climbing Stairs