Assignment 3

Due Friday February 1, 2013 at noon.

Submit a single zip file to cuLearn with all of your answers (1 file for the written part and 1 file for each Processing sketch).


Part 1: Write your answer in plain English. Put all of your answers in one single text file (.txt) or a single PDF file (.pdf).

Note: Please submit your written work in a single .txt or .pdf file. Your work will not be graded if it is not submitted in one of these two formats.


  1. Consider the following code fragment:

    int x = 3;
    int y = 4;
    int z = 2*x+y;
    if ( (x+y > 10) || (z > 10) ){
       z = z + 1;
    }else{
       z = (x+y)/2;
    }
    
    What is the value of z at the end?
  2. Recall the Kinder Surprise Eggs question from Assignment #1. Assume that there are n eggs (n > 5). All eggs weigh the same except one single bad egg (missing its toy) and you can put as many eggs on each side of the balance as you wish.

    Give an algorithm that can always find the bad egg with less than n/2 comparisons. (For large values of n, your algorithm should require much less than n/2 comparisons.)

    Note: If your algorithm from Assignment #1 satisfies this condition, when generalized to arbitrary number of eggs, then you can reuse your solution here.

  3. The local wizard, OT, has a bad habit of shrinking people and putting them in small boxes. OT has a special room where all the boxes (with all the shrunken) are located. Each box has the name of the person inside it written on it (and there is only one person inside any box).

    Each year, OT randomly picks one person (that has not been shrunken and boxed) and lets them free one person who has been shrunken and boxed. You are that lucky person this year. (And you are happy about this since you best friend was shrunken and boxed two years ago!)

    Describe an algorithm of how you would find your best friend's box in the room. What is the least possible number of boxes you need to check in order to find your friend? What is the worst possible number of boxes you need to check in order to find your friend?


Part 2: Write a Processing sketch as an answer for each of the following questions or tasks, unless otherwise stated.

Note: For all of your sketches, the output window should have width and height at least 200 (but do not make them extremely large).

Note: Do NOT use setup() and/or draw() unless specifically asked to.

  1. Plot the function y = x*cos(x) + sin(x)/x from x=-10 to x=+10. You should fit the plot nicely to your output window (regardless of the size of your output window). You should draw the x-axis and y-axis on the plot as well. Your plot should look nice (i.e., it should look smooth).

  2. You get a part time job as a door-to-door vacuum salesperson. You can make at most 1 sale per hour and each sale earns you either $75, $125, or $400. You do not want to work more than 100 hours a week. If you happen to make $9,000 or more before working 100 hours you stop working.

    Write a sketch that randomly simulates a week's pay for you. You can assume that you make a sale 50% of the time. Each time you make a sale, it is equally likely that you make $75, $125, or $400.

    Your sketch should print to the output window a message saying how many hours you worked and how much money you made this week.

  3. Generate 7 random points on the screen. Each point should be visualized with a small ellipse (width/height something like 10). Draw a line connecting each point to every other point.

  4. Using setup() and draw(), write a sketch that has a circle moving back and forth (horizontally) across the screen. The circle should change direction when it hits the boundary of the output window. The entire circle should be entirely visible in the window at all times.

  5. Using setup() and draw(), write a sketch that has a circle moving back and forth (horizontally) across the screen. The circle should start from the centre of the output window and move to the right. After the circle has "bounced" three times off of a boundary, it no longer bounces and just goes through the next wall (boundary) it collides with.

  6. Using setup() and draw(), write a sketch that draws a circle on the output window. When the mouse is over the circle, the colour of the circle should change. When the mouse leaves the circle, the colour should go back to the original colour.

    Note: It doesn't matter what colours you use (unless specified). Be sure that the change in colour is dramatic enough that it is easily observable.

  7. Using setup() and draw(), write a sketch that draws a square on the screen at a random location. Whenever the mouse moves on top of the square, have the square move to a new random location.