CSC 160 Homework 6

Assigned Oct. 20, due Nov. 7

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 (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 and turning it in.

This assignment is to be done in pairs, just like previous homeworks, but with a different partner.

Background:

Recall that the posn structure is already defined in DrScheme. But if it weren't, we could have defined it as follows:

; A posn consists of two numbers, x and y.
(define-struct posn [x y])
; The define-struct automatically gives us the following functions:
; make-posn : number number => posn
; posn-x : posn => number
; posn-y : posn => number
; posn? : object => boolean
To make sure we're comfortable using these functions, let's write some examples.
(make-posn 3 4) "should be itself, i.e. (make-posn 3 4)"
(define here (make-posn 5 12))
(posn-x here) "should be" 5
(posn-y here) "should be" 12
(posn? here) "should be" true
(posn? 7) "should be" false

Now that we've defined this data type, we can write a variety of functions that work with it. As a warm-up to writing real functions, we'll write template functions function-for-posn and function-returning-posn, which aren't "real" functions, but patterns that can be copied and pasted every time you have a real function to write. They look like this:

(define (function-for-posn the-posn)
   the-posn                           ; a posn
   (posn-x the-posn)                  ; a number
   (posn-y the-posn)                  ; a number
   )
(define (function-returning-posn whatever)
   (make-posn some-number another-number))
For example, if we wanted to define the distance-to-0 function, which takes in a posn, we would start by copying and pasting function-for-posn and changing the name to get
(define (distance-to-0 the-posn)
   the-posn                           ; a posn
   (posn-x the-posn)                  ; a number
   (posn-y the-posn)                  ; a number
   )
We would then put these ingredients together using the distance formula to get
(define (distance-to-0 the-posn)
;  the-posn                           ; a posn
;  (posn-x the-posn)                  ; a number
;  (posn-y the-posn)                  ; a number
   (sqrt (+ (sqr (posn-x the-posn))
            (sqr (posn-y the-posn))))
   )
This sequence of steps is described in Design Recipe, version 3.

The assignment

Grading standards

Error log:       /15

account struct Definition:   /5 Contracts:   /5 Examples:   /5 Templates:   /10
compute-interest Contract:   /5 Examples:   /5 Definition:   /10 Results:   /5
deposit Contract:   /5 Examples:   /5 Definition:   /10 Results:   /5
candidate struct Definition:   /5 Contracts:   /5 Examples:   /5 Templates:   /10
who-won Contract:   /5 Examples:   /5 Definition:   /10 Results:   /5
collect-votes-4 Contract:   /5 Examples:   /5 Definition:   /10 Results:   /5
4-votes->winner Contract:   /5 Examples:   /5 Definition:   /10 Results:   /5

General skills:

Following directions /20
Writing contracts from word problems /20
Choosing examples /20
Choosing names /20
Coding /20
Code re-use and choice of auxiliaries /20

Total:         /310


Last modified:
Stephen Bloch / sbloch@adelphi.edu