[ create a new paste ] login | about

Link: http://codepad.org/b9xLpE6y    [ raw code | output | fork ]

PhoeniX888 - C, pasted on Sep 10:
//Write a program that does a 3-way swap. 
//The function threeWaySwap(a,b,c), will put the value of a in b, b in c, and c in a.
//Without using extra variable

#include <stdio.h>

void threeWaySwap(int *a,int *b,int *c);
void swapTwo(int *x, int *y);

int main ()
{
  int a=1,b=2,c=3;
  threeWaySwap(&a,&b,&c); 
  printf("a = %d, b = %d, c = %d" , a,b,c);
  return 0;
}
void threeWaySwap(int *a,int *b,int *c)
{
  swapTwo(a,b);
  swapTwo(a,c);
}
void swapTwo(int *x, int *y)
{
  *x=*x+*y;
  *y=*x-*y;
  *x=*x-*y;
}


Output:
1
a = 3, b = 1, c = 2


Create a new paste based on this one


Comments: