CSC 171
Homework 5

Assigned Mar 10, due Mar 20

Turning in animation assignments

This assignment includes only one animation problem, so you should be able to write all of your answers in one Definitions window. As before, your Interactions window should still show me the results of test cases on functions by themselves (before you use them in an animation).

Programming

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 e-mail me this file. Also test your program: since you've already included examples in the Definitions window, you should be able to hit the Run button and see all the results (along with what you said they "should be"). Save the resulting Interactions window to a text file and e-mail 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.

  1. Imagine a program written to help a professor keep track of student grades. For now, grades will be stored as a list of strings, one for each student. Develop the following functions:

  2. This isn't a realistic way to keep track of grades. Define a data type student which contains a string (name), a number (id), and another string (letter-grade). As usual, provide a data definition in English, a define-struct, examples of the new data type, contracts for the functions that come for free with it, and input and output templates.

    Now develop the following functions that operate on lists of students:

    count-failing-students : student-list -> number
    counts how many students have a letter grade of "F".
    pass-all-students : student-list -> student-list
    changes every "F" letter grade to a "D".
    remove-failing-students : student-list -> student-list
    removes from the list every student with a letter grade of "F"
    sort-students-by-id : student-list -> student-list
    sorts the students in increasing order by student ID number
    sort-students-by-grade : student-list -> student-list
    sorts the students in order by letter grade, with the A's appearing first and the F's last. Note: you'll need the built-in string<? function, which you can look up in the DrScheme Help Desk.
  3. Develop the following functions on natural numbers:

    copies : natural-number object -> list
    Takes in a natural number and another object (a string, a number, an image, whatever) and returns a list with that many copies of the object. For example,
        (copies 3 "hello")
        "should be"
        (cons "hello" (cons "hello" (cons "hello" empty)))
        
    count-down : nat-num -> list-of-numbers
    which takes in a natural number and returns a list of the natural numbers from that number down to 0. For example,
        (count-down 4)
        "should be"
        (cons 4 (cons 3 (cons 2 (cons 1 (cons 0 empty)))))
        
  4. Recall the animation exercise from homework 4 about a list of shapes, which were defined to be either circles or rectangles. Let's add motion to that animation:

    1. Develop a function move-shapes which takes in a list of shapes and two numbers (dx and dy), and moves every shape by that much. (Hint: re-use a function from homework 3!)

    2. Modify your handle-key function from homework 4 so that when the user presses the up, down, left, or right arrow key, all the shapes move a few pixels in that direction. (The c, r, and - keys should still work as they did in homework 4.)

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 or struct name Contract Examples Definition Test runs
pass-everybody function /5 /5 /10 /5
substitute-string function /5 /5 /10 /5
pass-everybody-again function /5 /5 /10 /5
remove-fails function /5 /5 /10 /5
student struct data def'n  /5 define-struct  /5 contracts  /5 templates  /10
count-failing-students function /5 /5 /10 /5
pass-all-students function /5 /5 /10 /5
remove-failing-students function /5 /5 /10 /5
sort-students-by-id function /10 /10 /20 /10
sort-students-by-grade function /10 /10 /20 /10
copies function /5 /5 /10 /5
count-down function /5 /5 /10 /5
move-shapes function /5 /5 /10 /5
handle-key function /5 /5 /10 /5

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:         /550


Last modified:
Stephen Bloch / sbloch@adelphi.edu