This preview shows pages 1–13. 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: University of Waterloo
Midterm Examination #1
CS 135 Term: Winter Year: 2012 Identifying information will be printed here. Date: January 30, 2012
Time: 7:00 pm
Duration: 1 10 minutes
Sections: 001—002
Instructors: Brad Lushman Student Signature: UW Student ID Number: Number of Exam Pages  _ ages
(including this cover sheet) \ C
Exam Type I Closed Book
/
Additional Material Allowed ) NO ADDITIONAL MATERIALS ALLOWED This document is for the exclusive use of x8wen. Marking Scheme (for graders’ use only): Question Max Score Marker 1 11 2 12 U.) 15 4 20 5 14 Maximum Total Segre? 72 Total Score: x.“ “a Instructions: 3 ' o All code is to use the Beginning ‘~ tudent language. 0 Individual questions will indicate \ hichesign recipe components must be included for full marks. Unless
otherwise speciﬁed, you can assu e the following: — Helper functions must also include a purpose (in addition to any other design recipe components
mentioned in the question). — Examples and tests must use checkexpect, and unless otherwise allowed, must be different from any
examples supplied in the question. 0 Your functions do not have to check for invalid inputs unless we ask you to do so. 0 Functions you write may use:
— 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 or special form discussed in lecture, unless speciﬁcally noted in the
question. — Any builtin mathematical function. CS 135 Midterm Examination #1 Winter 2012 Page 2 of 13
This document is for the exclusive use of x8wen. l. (11 points) (a) (4 marks) There are several formulas for ﬁnding the area of a triangle, depending on what
information you are given. For example, if you are given two sides a and b, and the angle 6 between them, you can calculate the area as
ab sin 6' 2 Write the Scheme function triangleareaSAS that consumes side lengths a, b, and angle 6 (given in
degrees), in that order, and produces the area of the triangle given by these parameters. You do not
need to declare any constants. Do not include a contract, purpose, examples, or tests. Note: the Scheme expression (sin x) produces the sine of the angle :6, where a". is measured in radians.
An angle in degrees can be converted to radians by muliplying by fr/180. The constant pi is
predeﬁned in Scheme. (deﬁne (triangle—areaSAS a b theta)
(/ (>I< a b (sin (>I< theta (/ pi 180)») 2)) ) (b) (3 marks) On the other hand, Etheth'r
computed by Heron’s formxla:  < side of the triangle are given, then its area may be 8(8 — a)(s —b)(s — c), where the quantity .3 is equal to (y—k b + c)/2. Write the Scheme function tr'angieareaSSS that consumes side lengths as, b, and c, and produces the
area of the triangle given b hese parameters. You do not need to declare constants. Do not include
a contract, purpose, examples, or tests. (deﬁne (triangle—areaSSS a b C)
(squ (>1< (/ (+ a b C) 2)
(— (/ (—— a b C) 2) a)
(— (/ (—— a b C) 2) b)
(— (/ (— a b C) 2) 6)») CS 135 Midterm Examination #1 Winter 2012 Page 3 of 13
This document is for the exclusive use of x8wen. (c) (4 marks) Have you read the instructions at the beginning of this exam? If not, ﬁrst do that. Now
write a general purpose Scheme function trianglearea that computes the area of a triangle, where the
dimensions of the triangle can be speciﬁed in either of the above two ways. This function should
consume four arguments. The ﬁrst argument is a symbol, and will be either ’SAS or ’888. If the
ﬁrst argument is ’SAS, then the remaining three arguments will be a, 9, and b, in that order, as given
in part (a) of this question. If the ﬁrst argument is “888, then the remaining three arguments will be a, b, and c, as given in part (b) of this assignment. In either case, your function should produce the
area of the triangle as speciﬁed by these three arguments. Include a contract and purpose for your main function. Do not include examples or tests. (deﬁne (trianglearea type dimension} dimension2 dimension3) ;; triangle—area: Symbol Num Num Num —>\ u%
;; [(union ’SAS ’SSS) instead of Symbol i O ;; Produces the the area of the triangle i ﬁven dimensions, using SSS
;; formula if type is ’SSS, SAS f rm ai ._ p
(g e is ’SAS
(deﬁne (triangle—area type di en. dimensionZ dimension3)
(cond __
[(symbol:? type ’ A63} 2
(triangle—are__ct—.Sl<45 tm nsionI dimensionj’ dimension2)]
[(symbol:? type.5888) (triangle—area—SgS dimension] dimensionZ dimension3)])) CS 135 Midterm Examination #1 Winter 2012 Page 4 of 13
This document is for the exclusive use of x8wen. 2. (12 points) Assume that the following deﬁnitions have already been completely processed in Beginning
Student: (deﬁne x ‘l 0)
(deﬁne y (* x (+ x 1)»
(deﬁnestruct s (x y)) (deﬁne (f x y z)
(cond [(> X y) (H7 Z)]
[(odd? y) (even? x)]
[else ED) For each of the following Scheme expressions, provide the following two lines: o The expression that would result after exactly one substitution step, using the substitution rules as
deﬁned in lectures; and o The ﬁnal value that would result. If the evaluation would rel ltmgerror, describe the error.
(Each part is worth two points.) K
(a) (even? y) ( 2) \
(even?110)  \
(y \ true i9 ('3) (fxy(makeS(+xy)(—Xy‘ ) / (f 10 y (make‘5‘ (+ X y) (— x )0)»
(make—s 120 —100) CS 135 Midterm Examination #1 Winter 2012 Page 5 of 13
This document is for the exclusive use of x8wen. (c) (f 3 3 (makes 7 7)) (cond [(> 3 3) (Sx (make—S 7 7))] [(Odd? 3) (even? 3)] [else (make—
S 7 7)])
false (d) (f 6 5 (makes 4 3)) (cond [(> 6 5) (Sx (make—S 4 3))] [(Odd? 5) (even? 6)] [else (make—
S 4 3)])
4 (e) @765) 5 x
( (cond [(> 7 6) (S—x 5)] [(odd? 6 (e1, .n? 7)] [else 5])
\ ._ Error (5 is not a struEtuﬁe).\ (1') 007500755» /
(f 7 6 (cond [(> 7;) (s—x 5)] [(odd? 6) (even? 7)] [else 5])) Error (5 is not a structure). CS 135 Midterm Examination #1 Winter 2012 Page 6 of 13
This document is for the exclusive use of x8wen. 3. (15 points) Consider the following Scheme function: (deﬁne (mystery w x y 2)
(cond
[(< y 0) (+ x z)]
[(< x 20) (cons x w)]
[(<: y 30) "BOO!"]
[else (and (< xy) (< x ZDD) (a) (3 points) Give the contract that most closely ﬁts this function. (listof Num) Num Num Num —> (union Num (listof Num) String
Boolean) (b) (8 points) Give a complete set of tests that fully exercise this function. Express your answer as a
sequence of checkexpects. For each test, brieﬁy state its purpos i.e, which part of the function is
being tested). The ﬁrst one is done for you: (checkexpect (mystery empty 1 —5 1) 2) ; ﬁrst cond bran (check—expect (mystery empty ) "BOO!”) ; third cond branch
(check—expect (mysteryﬁmhy 1 ) false) ; fourth cond branch, ﬁrst 013]
(check—expect (mystegy m y 35 1) false) ; fourth cond branch, second i
Cnﬁy 25 35 45) true) ; fourth cond branch, all clau
(check—expect (mys ery/empty 1 0 1) (cons 1 empty)) ; ﬁrst branch boundar)
(check—expect (mysﬁry empty 20 1 1 ) "BOO!”) ; second branch boundary
(check—expect (mystery empty 20 30 0) "BOO!”) ; third branch boundary (check—expect (mystery empty 2 )i) eggs 2 empty)) ; second cond branch
1 (check—expect (mysliery CS 135 Midterm Examination #1 Winter 2012 Page 7 of 13
This document is for the exclusive use of x8wen. (c) (4 points) Consider the following rearrangement of the function mystery, in which the branches of the
cond have been presented in reverse order: (deﬁne (mystery x y Z) (cond
[ ————————————————————————————————————————— —— (and (< X)’) (<XZ))]
[ _________________________________________ __ "Boo!”]
[ __________________________________________ __ (cons x w)]
[ ————————————————————————————————————————— —— (+ x Z)])) Fill in the blanks above with conditions, such that this version of mystery behaves like the original. (and (> y 30) (>: x 20)) (mmt>:you>:x2m) ‘§;\>
(g) (>: y 0)
else \
C “x \X
_1_\ ' 9. ) / CS 135 Midterm Examination #1 Winter 2012 Page 8 of 13
This document is for the exclusive use of x8wen. 4. (20 points) Suppose you wish to write a program to keep track of your upcoming assignments. For each
assignment, you wish to record which course the assignment goes with, how much the assignment is worth (expressed as a percentage of the ﬁnal grade), the number of days before it is due, and the estimated
number of hours to complete it. (a) (3 points) Give a structure deﬁnition for an assignment structure with ﬁelds course, weight,
daysﬂllDue, and timeNeeded. (deﬁnestruct assignment (course weight daysTillDt/te time—
Needed» (b) (5 points) Complete the following data deﬁnition for assignments: ;; An assignment = ( — — — —) make—assignment String Nat Nat Nat \ ' x
(c) (4 points) Give a template for functions that consume an assggnen .
(deﬁne (my—assignment—ﬁt a. ang yer/Lt)
. .. (assignment—coursew igm emf) .. .
. .. (assignmentweigg ass’gnment) .. .
. . . (assignmenf—dhysTil D e anassignment) . . .
. .. (assignmentrtﬁite eeded anassignment) .. . ) \/ (d) (4 points) Time is tight, and , ou want to make effective use of the time you have. Write the scheme
function highpayoﬂ‘? that c sumes an assignment and produces true if the assignment is worth at
least 10 percent of your ﬁnal grade, and can be completed in 3 hours or less. Do not include the contract, purpose, examples, or tests. (deﬁne (high—payoﬂ? anassignment)
(and (>: (assignment— weight anassignment) 10)
(<: (assignment—timeNeeded anassignment) 3))) (e) (4 points) Write the function lostcause? which consumes an assignment and produces true if the
amount of time required to complete the assignment is greater than the amount of time before it is due. Do not include the contract, purpose, examples, or tests. CS 135 Midterm Examination #1 Winter 2012 Page 9 of 13
This document is for the exclusive use of x8wen. (deﬁne (lost—cause? anassignmenr)
(> (assignment—hoursNeeded anassignment)
(* (assignment—daysTillDue anassignmem) 24)» CS 135 Midterm Examination #1 Winter 2012 Page 10 of 13
This document is for the exclusive use of x8wen. 5. (14 points) When an iClicker vote is taken, the incoming votes are grouped by answer (A, B, or C)‘ so that,
upon completion of the vote, we can display a histogram of responses. In this question, you will write a function to tablulate clicker votes by response. You will use the following structure deﬁnition and data deﬁnition: (deﬁnestruct histogram (as bs cs))
;; A Histogram = (makehistogram Nat Nat Nat) (a) (6 points) Write the Scheme function oneanswer, which consumes a histogram and a response (the
response will be one of ’A, ’B, or ’C), and produces the histogram resulting from adding the response to the appropriate category. For example, (oneanswer (makehistogram 2 5 3) ’B) should produce
(makehistogram 2 6 3). Include a contract. ;; one—answer : Histogram Symbol —> Histogram
(deﬁne (one—unwer his! ans) (cond 
[(symbol:? ans ’A) \ ' \
(makehismgmm  x
(+ 1 (hismgmmagig?!
(his!0gmm— s h! !) __
(his!0gm C [(symbol: ? (13% ’ AB (makehism rm
(h\’s!0g€'m —as his!) 1 (hismgmm—bs his!))
(hi. mgram—CS his!))]
[(symbol: ? ans ’C)
(makehismgmm
(his!0gmm—as his!)
(his!0gmm—bs his!)
(+ 1 (hismgmmcs his!))])) 1D and E omitted in the interest of brevity. CS 135 Midterm Examination #1 Winter 2012 Page 11 of 13
This document is for the exclusive use of x8wen. (b) (8 points) Write the Scheme function processanswers, which consumes a list of answers, and
produces a histogram of all answers received. For example, (processanswers (cons “A (cons ’ B
(com ’0 (cons ’A empty))))) should produce (makehistogram 2 '1 '1). Include a contract and at least
three tests (or examples), different from the given example. Be sure that your tests or examples COVCI’ the necessary CELSBS. ;; process—answers : (listof Symbol) —> Histogram
(deﬁne (process—answers answers) (cond
[(empry? answers) (make—histogram 0 0 0)] [else (one—answer (processanswers (rest answers» (ﬁrst answers))]))
(check—expect @FOCBSSanSi'VEFS empty) (make—histogram 0 0 0))
(check—expect (process—turns (cons ’A emth (make—histogram 1 0 0)) \
(check—expect (process—turns (cons n ’8 empty)»
0 j (make—histogram 2 \ a _\)
_\ “ <I ) / CS 135 Midterm Examination #1 Winter 2012 Page 12 of 13
This document is for the exclusive use of x8wen. (Extra space for any question, if needed) CS 135 Midtelmrdoaument is fothheremclusive use of x8wen. Page 13 of 13 ...
View Full
Document
 Winter '07
 VASIGA

Click to edit the document details