Aliasing exercise

  1. Write the function

    void swap(int a, int b)

    meant to swap the values of a and b. That is, if I say

    void setup()
    {
      int x = 5;
      int y = 10;
    
      swap(x, y);
    
      println(x);
      println(y);
    }

    I should expect to see 10 followed by 5 in my output.

    You actually won’t be able to get this to work. But write the swap function anyway, and write printlns in your swap function to demonstrate that it really does swap a and b, even though the x and y in setup do not get swapped.

  2. Write the function

    void swap(int[] a, int[] b)

    where a and b are assumed to be 1-element arrays. (That is, a.length and b.length are both 1.) Set up a similar situation to problem (1) in your setup function to test your new swap function. Here, you should observe that your swap function does work.

  3. Write the function

    void swap(MutableInteger a, MutableInteger b)

    where you have pasted the code from the MutableInteger class into a second tab in your Processing window. (It’s available on our course webpage.) This also should work.