Tuesday, July 28, 2009

C# quiz (solutions in other languages might be accepted)?

Write the body of the following method:





public static void Swap(ref long left, ref long right)


{


}





...so as to swap the values in left and right but you may not declare another variable or invoke another method/constructor - so the following is illegal:





var temporary = left;


left = right;


right = temporary;

















_

C# quiz (solutions in other languages might be accepted)?
left ^= right;


right ^= left;


left ^= right;
Reply:write a function like this...


public void swap(int x, int y)


{


MessageBox.Show("x=" + x + ",y=" + y);


x -= y;


y += x; // y gets the original value of x


x = (y - x); // x gets the original value of y


MessageBox.Show("x=" + x + ",y=" + y);


}





and then call it in side a button click...like





private void button1_Click(object sender, EventArgs e)


{


swap(5000, 10000);


}





It ll work...


Enjoy.
Reply:Choose me for the best answer:


{


left+=right;


right=left-right;


left-=right;


}


No comments:

Post a Comment