This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Warning The content of 08135
changes from term t This exam may Ora gym] not
correspond toth ﬂuent
content of t \course and thus
may diffe_r\___s___I__ ificantly from
the exam you will be expected
to write. This document is for the exclusive use of x8wen. This document is for the exclusive use of x8wen. UNIVERSITY OF Waterloo University of Waterloo
Midterm Examination #2
Term: Fall Year: 2008 Student Name UW Student ID Number Course Abbreviation and Number
Course Title Section(s) Instructors (Please circle your instructors name!)( 2D CS 135
Designing Euze%lam Pro
001 —011 my Vasiga @911" ecker, Ian Goldberg, Brad Lu shman, Time Period ) Date of Exam Duration Number of Exam Pages
(including this cover sheet) Exam Type Additional Material Allowed (”October 27, 2008 Start time: 7:00 pm End time: 9:00 pm
120 minutes 12 pages Closed Book UW—Approved Calculators This document is for the exclusive use of x8wen. CS 135 Midterm Examination #2 Fall 2008 ID Number: Marking Scheme (for graders’ use only): Question Maximum Score 1 7 6 15 (r \\ 
MaxicmquBB S ore: 82 Some important notes: HH/ 0 Regarding design recipes: )
— For functions that we ask you to write, you must include a contract but are not required to
include the purpose, examples, or tests, unless otherwise speciﬁed. — For helper functions you create, you must include contract and purpose but are not required to
include examples or tests. — You do not need to write out data analysis or templates unless we speciﬁcally ask for it. 0 Functions you write may call: — Any function you have written in another part of the same question — Any function we asked you to write for a previous part of the same question (even if you didn’t
do that part) — Any other builtin function discussed in class (with the exception of reverse and append) CS 135 Page 2 of 12
This document is for the exclusive use of x8wen. CS 135 Midterm Examination #2 Fall 2008 ID Number: 1. (7 points) Draw “boxandpointer” pictures corresponding to the following structures: (a) (2 points) (cons 3 (cons 4 (cons ”blue empty))) (b) (5 points) (list (list ‘I 2) (list 3 (list 4) empty 5)) Q 2. (6 points) For each of the following four constant deﬁ 'tions, (_wylin alternate (but equivalent) deﬁnition
that does not use cons or empty: \ (a) (1 point) (deﬁne 0 empty) _ \
< ) (b) (1 point) (deﬁne b (cons. em\_1_ty emp
) / (c) (2 points) (deﬁne c (cons (cons 1 empty) (cons (cons 2 empty) empty))) (d) (2 points) (deﬁne d (cons (cons 4 (cans "four" (cons ’four empty))) empty)) CS 135 Page 3 of 12
This document is for the exclusive use of x8wen. CS 135 Midterm Examination #2 Fall 2008 ID Number: 3. (7 points) Write the function max3 lists which consumes three lists of numbers (all with the same length) and produces a list where the number at position i is the maximum of the numbers at position i of the consumed lists. For example, (maxj’lists (list '1 '10 100) (list 4 ‘I 99) (list 3 9 ‘I 01)) should produce (list 4
10 101). If the lists are not the same length, you should evaluate (error ’max3lists “Inputs are not
same length“). CS 135 Page 4 of 12
This document is for the exclusive use of x8wen. CS 135 Midterm Examination #2 Fall 2008 ID Number: 4. (27 points) A note is a structure (makenote pitch dur), where pitch is a symbol and dur (for “duration”) is a
positive number.
A silence is a structure (makesilence dur), where dur is a positive number.
A melody is a nonempty list of notes and silences which ends with a note. (a) (5 points) Write structure deﬁnitions for note and silence, and a data deﬁnition for melody. 3\ (b) (5 points) Write a template for a function th\t\con umes a melody  \
Q
__\__ _ ) CS 135 Page 5 of 12
This document is for the exclusive use of x8wen. CS 135 Midterm Examination #2 Fall 2008 ID Number: (c) (7' points) Write a scheme function countsilence which consumes a melody and produces the sum of
the durations of all the silences in the melody. CS 135 Page 6 of 12
This document is for the exclusive use of x8wen. CS 135 Midterm Examination #2 Fall 2008 ID Number: (d) (10 points) A melody is called “monotone” if every note in the melody has the same pitch. Write a
predicate function monotone? which consumes a melody and produces true if and only if the melody :9
(
\ J O \\ \ " C )/._ This document is for the exclusive use of x8wen. CS 135 Midterm Examination #2 Fall 2008 ID Number: 5. (20 points) A natural number n is called a perfect number if the sum of the distinct positive factors of to (not
including to itself) is equal to n. For example, 28 is a perfect number because its factors are l, 2, 4, 7, and
14, which sum to 28. Some other perfect numbers are 6, 496, and 8128. (a) (6 Points) Write a function factors<=i which consumes two natural numbers, n and 3', and produces
a list of all the factors of n, which are at least 1 and less than or equal to 3'. The list can be in any order
but should not have any duplicates. CS 135 Page 8 of 12
This document is for the exclusive use of x8wen. CS 135 Midterm Examination #2 Fall 2008 ID Number: (b) (7' Points) Write a function perfect? which consumes a natural number n 2 2 and produces true if n
is a perfect number and false otherwise. Include a contract of perfect?, and both a contract and a
purpose (but not examples or tests) for any helper functions. CS 135 Page 9 of 12
This document is for the exclusive use of x8wen. CS 135 Midterm Examination #2 Fall 2008 ID Number: (c) (7' Points) Although every known perfect number is even, an odd perfect number might still exist.
Computers are useful to search for such things. Write a function oddperfecrsearch which consumes
two odd positive integers a. and b and produces the smallest odd perfect number in the range from a. to
b (inclusive), or false if none exists. You should include a data deﬁnition for odd positive integers. (d) (10300 Points Bonus) Give an example of an odd perfect number. CS 135 Page 10 of 12
This document is for the exclusive use of x8wen. CS 135 Midterm Examination #2 Fall 2008 ID Number: 6. (15 points) Write a function listlisIpick which consumes a list of lists of numbers and a natural number
n, 2 1 and produces the nth number in the list of lists (counting each number in each list), or false if there
aren’t enough numbers in the list of lists. For example, the function call (listlistpick (list (list 4 2O 3) (list 16 82 29 8)) 5)
should produce the value 82. CS 135 Page 11 of 12
This document is for the exclusive use of x8wen. CS 135 Midterm Examination #2 Fall 2008 ID Number: (Extra space for any questions, if needed) CS 135 Page 12 of 12
This document is for the exclusive use of x8wen. ...
View
Full Document
 Winter '07
 VASIGA

Click to edit the document details