CS 246 Homework 2

Due 11:59 AM, Monday April 9

  1. Write a program called rw that draws a 2-dimensional random walk in a window. In a random walk, we keep track of an x coordinate and a y coordinate for some number of steps in a random direction. This can be done with two variables. At each step, just increment or decrement both x and y according to a random choice. Then draw a pixel (1x1 rectangle) at the new position. The starting position will be the center of the screen.
  2. When the window is resized, it must redraw the same random walk with a new center. What does this tell you about where to call srand48?
  3. Your program must take the following options:
    -s steps: the number of steps
    -S seed: a seed for the random number generator
    -r red: the red component of the color for steps
    -g green: the green component of the color for steps
    -b blue: the blue component of the color for steps
    -R red: the red component of the background color
    -G green: the green component of the background color
    -B blue: the blue component of the background color
  4. Remember that options with arguments (like all of these) are identified by putting a colon after the letter in the option string for getopt. When one of the options is recognized, the argument that follows it is available in the variable optarg. It is a string and must be converted to a number with atoi or atof.
  5. All variables should have reasonable defaults. 10000 for steps, the pid for the seed, white for the background and black for the steps.