This preview shows pages 1–12. Sign up to view the full content.
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 Document
Unformatted text preview: Warning The content of CS135
changes from term t ' erm.
This exam may or not
correspond to th '7 rrent
content of t “course and thus
may differiSI 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 #1
Term: Fall Year: 2009
Student Name
UW Student ID Number
Course Abbreviation and Number CS 135 \
Course Title Designing Fu ‘dofl ProU ams
Section(s) 001—007 " )
XX“ Instructors Biron WEber ecker, Ian Goldberg, Craig Kaplan, ArrieSt jo ann Date of Exam \ I g ;tober 5, 2009 Time Period ) / Start time: 7:00 pm End time: 9:00 pm
Duration 120 minutes
Number of Exam Pages 10 pages (including this cover sheet) Exam Type Closed Book Additional Material Allowed NO ADDITIONAL MATERIALS ALLOWED This document is for the exclusive use of x8wen. CS 135 Midterm Examination #1 Fall 2009 ID Number: Marking Scheme (for graders’ use only): Question Maximum Score 1 15 (
Maximum Total c0 Total C a? _. \ ' <7 ) \ B “a B Instructions: 0 All code is to use the Beginning the design recipe for each function you write. S)tudent language. 0 Unless otherwise speciﬁed, you must include the contract, examples, and function deﬁnition portions of — Helper functions must also include a purpose. — Examples must use checkexpect, and unless otherwise allowed, must be different from any examples supplied in the question. 0 You do not need to write out data deﬁnitions or templates unless we speciﬁcally ask for them. 0 Your functions do not have to check for invalid inputs unless we ask you to do so. 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, unless speciﬁcally noted in the question. CS 135 This document is for the exclusive use of x8wen. Page 2 of 10 CS 135 Midterm Examination #1 Fall 2009 ID Number: l. (15 points) Write Scheme deﬁnitions corresponding to the following mathematical expressions. You don"t
need to write the design recipe. as: + h) — my (a) (5 points) The constant of = f
‘3. Note: Assume that the constants 3: and h and function f are already deﬁned. (b) (5 points) The function dis.cm'rriinstill“(a= y‘ c) = y? — x ‘N. xxx \ <7 ) /
2 ifb2—4a.c>0 (c) (5 points) The function numdisn‘ncrreairoots(a., b‘ c) = 1 if b2 — 4ac : 0 .
0 ifb2—4a.c<0 CS 135 Page 3 of 10
This document is for the exclusive use of x8wen. CS 135 Midterm Examination #1 Fall 2009 ID Number: 2. (15 points) The dosage of vaccine that a veterinarian should administer to a dog depends on the mass of the
animal. For the ﬁrst 10 kg, the rate is 30 ml of vaccine per kilogram. Above 10 kg, the rate changes to
20 ml of vaccine per kilogram above 10 kilograms. Consider the function dosage which takes as input the
weight of the dog in kg, and outputs the corresponding dosage of vaccine in ml. Negative inputs should
output 0. For example, (dosage 8) :)> 240 and (dosage 10.5) :> 310. (a) (2 points) Give a contract for dosage. (b) (8 points) Give the code for dosage. If you use any helﬁfuﬁgms, include contracts and purposes (but not examples or tests) for them. ( J
.. K CS 135 Page 4 of 10
This document is for the exclusive use of x8wen. CS 135 Midterm Examination #1 Fall 2009 ID Number: (c) (5 points) Using checkexpect, give tests for dosage that completely exercise your code. Indicate in a
comment for each test which case is being covered. (If you didn’t ﬁnish the code on the previous
page, give test cases that you think would cover all important cases.) ‘\
x V) C “N 3. (10 points) For each of the following SciE'me e} pressions, write an equivalent Beginning Student expression
that uses exactly one cond. \  Each of the functions p! ?._ pZ ?. etc. are redicates with the contract Hum —> booiemz and each of the
functionsﬂ,f2._ etc. have the contjact mm. —> num. (a) (3 points) (cond [(p!? x) (f! x)]
[(P2 5’ x) (f2 D] [else (cond [(p3? x) (f3 )0] [(P4 5’ x) (f4 J6)] [else 0‘5 x)])]) CS 135 Page 5 of 10
This document is for the exclusive use of x8wen. CS 135 Midterm Examination #1 Fall 2009 ID Number: (b) (3 points)
(cond [(p!? x) (cond [(p3? x) (ff x)]
[010? (P3? J0) (f2 X)])]
[(p2 ? x) (cond [(p4? x) 0‘3 x)]
[(nor (W x» (f4 x)])]
[else 0‘5 x)]) x VF (c) (4 points) h a“.
(cond [(cond [(p!? x) (p2 33.x)” KB
[023.920 (Mm . '
[elseﬁzise ) < > [(P5? x) (f2 )3]
[else 05’ x)]) ) CS 135 Page 6 of 10
This document is for the exclusive use of x8wen. CS 135 Midterm Examination #1 Fall 2009 ID Number: 4. (10 points) Four numbers form a doubts product if the product of two of them equals the product of the other
two (in some order). Write a predicate doubtsproduct? that consumes four numbers and produces true if
and only if the numbers form a double product. For example, 0 (doubleproduct? 2 3 6 4) :)> true because 2 X G = 3 X 4
o (doubleproduct? 6 10 3 5) :> true because 6 X 5 = 3 X 10 o (doubleproduct? 8 4 2 6) :>faise because no pair of those numbers can be multiplied together to get
the same answer as the product of the other two. CS 135 Page 7 of 10
This document is for the exclusive use of x8wen. CS 135 Midterm Examination #1 Fall 2009 ID Number: S. (15 points) Given that the following deﬁnitions have been processed in the Beginning Student language: (deﬁne r 6)
(deﬁne s (* r 2)) (deﬁne (f s p) (+ p r s»
(deﬁne (g x r) (cond [(and (> r 8) x) x] [else r])) show the stepbystep simpliﬁcation of each of the following expressions. A simpliﬁcation step consists of
one substitution as described by the semantic model of the Beginning Student language, as discussed in
class. When you cannot carry out any more simpliﬁcation steps, explain why. (a) (3 points) Simplify (f s (— 3 2)) ‘\
(/J (b) (3 points) Simplify (gfaisej) / CS 135 Page 8 of 10
This document is for the exclusive use of x8wen. CS 135 Midterm Examination #1 Fall 2009 ID Number: (c) (3 points) Simplify (g (or (> r s) S) S) (d) (6 points) Simplify (g (or (< r S) s) s) ( 8 CS 135 Page 9 of 10
This document is for the exclusive use of x8wen. CS 135 Midterm Examination #1 Fall 2009 ID Number: (Extra space for any question, if needed) CS 135 Page 10 of 10
This document is for the exclusive use of x8wen. ...
View
Full
Document
This note was uploaded on 04/02/2012 for the course CS 135 taught by Professor Vasiga during the Winter '07 term at Waterloo.
 Winter '07
 VASIGA

Click to edit the document details