CSC 160
Homework 2

Assigned May 31, due June 6

For all the programming assignments, be sure to follow the design recipe. Write your function contract, examples, and function definition in the Definitions Window, save it to a file, and send me this file. Also test your program: since you've already included examples in the Definitions window, you should be able to hit the Execute button and see all the results (along with what you said they "should be"). Save the resulting Interactions window to a text file and send me this file too. Be sure to choose meaningful names for functions and parameters, and watch for opportunities to re-use functions you, I, or the textbook have already written.

Also turn in a log of how many errors of different kinds you encountered in the assignment, with brief comments describing each one ("mismatched parentheses" is self-explanatory, but more complex errors might need more description). You may do this using the PSP forms, or simply by keeping track in a text file or on paper and turning it in.

If you need help with the functions that require images or animations, please see the documentation for the tiles-world teachpack.

Miscellaneous Programs

Building an animation

  1. There is a predefined Scheme function named random that produces random numbers. Read about it in the DrScheme Help Desk. Try some examples in the Interactions window. Develop a function named random-posn that takes in two numbers (max-x and max-y) and returns a posn whose x coordinate is chosen at random between 0 and max-x, and whose y coordinate is chosen and random between 0 and max-y. Note: In your examples, you won't be able to predict exactly what answer comes out, but you can say something like "should be a posn with 0 ≤ x < 20 and 0 ≤ y < 50".

  2. Develop a function named add-colored-dot which takes a posn, a color, and an image, and returns the same image with a dot of the specified color added at the specified location.

    You may assume that the given image has its pinhole at the top-left. (An easy way to construct such images is with the empty-scene function in the tiles-world teachpack. It takes in a width and height, like rectangle, and produces a solid white rectangle of those dimensions with the pinhole at the top left corner.
    In the example at right, I've used the nw:rectangle function in the tiles-world teachpack. It works like rectangle, but puts the pinhole at the top left rather than in the center. If you get an error message from using this function, try re-installing the latest version of tiles-world.plt.)


  3. Develop a function named add-smart-dot which takes an image (background), a posn (center), a number (radius), and another posn (new-point), and returns the same image with either a green dot or a red dot added at position new-point; the dot should be green if it is within radius of center, and red otherwise.

  4. Develop a function named add-random-smart-dot which takes an image (background), a posn (center), and a number (radius), and returns the same image with a randomly chosen dot added to it, green or red depending on whether it's within radius of center, as above.

  5. Run an animation which uses an image as its world (so the show-world function is incredibly easy); it starts with a box, and at each tick of the clock, it updates the world using add-random-smart-dot. Turn in not only the Scheme code you used to run the animation, but also a screen shot showing the animation after at least fifty clock ticks.

Grading standards

Error log:       /30
(I'm not grading on how many or how few errors you encountered, only on whether you recorded them adequately.)

Function name Contract Examples Definition Test results
counterchange /5 /5 /10 /5
checkerboard /5 /5 /10 /5
trip-cost & auxiliaries
  What would you need to change?
/15 /15 /30 /15
/5
carpet-price & auxiliaries, if any /10 /10 /20 /10
who-won /5 /5 /10 /5
within-distance? /5 /5 /10 /5
random-posn /5 /5 /10 /5
add-colored-dot /5 /5 /10 /5
add-smart-dot /5 /5 /10 /5
add-random-smart-dot /5 /5 /10 /5
animation /20

General skills:

Following directions /10
Writing contracts from word problems /10
Choosing examples /10
Choosing names /10
Coding /10
Code re-use and function composition /10

Total:         /500


Last modified:
Stephen Bloch / sbloch@adelphi.edu