CSC 160
Homework 4

Assigned June 14, due June 22

For all the programming assignments, be sure to follow the design recipe. For functions, 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.

Feel free to use either list or cons notation for your examples, whichever you prefer.

Note: no homework will be accepted after 22 June!

Essay

Re-read the adages page, and choose one (or several closely-related) adages, or one longer article, that mean more to you than they did at the beginning of the term.
Write an essay of one or more well-structured paragraphs: what does it really mean in practice? Do you agree or disagree? Is it relevant or useful in your own field or career? Support your claims with specific examples from your own programming experience.

Miscellaneous list functions

Lists of employees

Copy the definition of the emp structure, and the templates template-for-emp and template-for-emps from June 14 examples. Using these templates as appropriate, develop the following functions:

Natural numbers

Computerized Elections

  1. Recall the candidate structure from homework 3. Write a function template for functions that take in a list of candidate structures.

  2. Develop a function named add-vote-for which takes in a string (the name of a candidate) and a list of candidate structures, and returns a list of candidate structures in which that candidate has one more vote (and all the others are unchanged). You may assume that no name appears more than once in the list of candidate structures.
    Note: it's possible that the name doesn't appear in the list at all (e.g. it's a write-in vote, or something like that); if so, the resulting list should have 1 vote for that candidate.

  3. Develop a function named tally-votes which takes in a list of strings (Voter 1's favorite candidate, Voter 2's favorite candidate, etc.) and produces a list of candidate structures in which each candidate name appears once, with how many votes were cast for that candidate.

  4. Develop a function named sort-candidates which takes in a list of candidates and sorts them in order by votes, so that the one with the most votes is first and the one with the fewest votes is last. (Requires Chapter 12.)

Building an animation

Let's keep building on that animation of the rocketship. First, we'll keep our rocketships from flying off the edges of the window. Then we'll animate several rocketships, not just one. (It would be really cool to get the rocketships to avoid crashing into one another, too, but that's more complicated.)

  1. Modify the handle-tick function from homework 3 so that if a rocketship goes off the window to the right, it re-appears on the left, and vice versa; if it goes off the window to the bottom, it re-appears at the top, and vice versa. Its velocity should not be affected: if it was moving right, it should still be moving right.

  2. Extra credit: modify the handle-tick function so that instead of teleporting from right to left edges, it uses its engine to avoid hitting the edge of the window. If it's within 20 pixels of the right edge of the window, it accelerates to the left as though you had hit the left-arrow key; if it's within 20 pixels of the top edge , it accelerates down as though you had hit the down-arrow key; and so on. Note that if it's going really fast, it might still hit (and go through) the wall; don't worry about this.

  3. Rename the handle-tick function (regardless of whether you did the extra credit) to update-rocket. Develop a new animation in which the world is a list of rockets. At each clock tick, apply update-rocket to all of them, so each one moves according to its own current velocity (and wraps around the edges, or avoids walls, individually). When you press an arrow key, all of them accelerate in the specified direction. And when you press the home key (recognizable in handle-key by the symbol 'home), it adds a new rocket at the center of the window, initially not moving.

    (Hint: This will require writing a new show-rockets function, modifying handle-key, rewriting handle-tick, and changing the call to big-bang so it starts with a list. Several of these tasks will be much easier if you use do-to-each, which requires the Intermediate Student language.)

Grading standards

Error log:       /20

Essay:       /50

Function or struct name Data def'n Define-struct Contracts for
struct functions
Template Contract Examples Definition Test runs
all-over-10? function   /5 /5 /10 /5
convert-reversed function   /5 /5 /10 /5
recall function   /5 /5 /10 /5
remove-first function   /5 /5 /10 /5
replace function   /5 /5 /10 /5
employee-list type given here  
count-over-100K function   /5 /5 /10 /5
fire-highly-paid function   /5 /5 /10 /5
sort-by-salary function   /10 /10 /20 /10
natural number type given in class  
table-of-squares function   /5 /5 /10 /5
increasing-table-of-squares function   /5 /5 /10 /5
random-posns function   /5 /5 /10 /5
candidate-list type /5   /15  
add-vote-for function   /5 /5 /10 /5
tally-votes function   /5 /5 /10 /5
sort-candidates function   /5 /5 /10 /5
rocket-list type /5   /15  
wraparound movement   /5 /5 /10 /5
avoiding walls by acceleration   /10 /10 /20 /10
animation functions   /15 /15 /30 /15

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:         /680 +    /75 XC


Last modified:
Stephen Bloch / sbloch@adelphi.edu