mid1sol - University of Waterloo Midterm Examination #1 CS...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 8
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 10
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 12
Background image of page 13
This is the end of the preview. Sign up to access the rest of the 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 specified, 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 check-expect, 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 built-in function or special form discussed in lecture, unless specifically noted in the question. — Any built-in 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 finding 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 triangle-area-SAS 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 pre-defined in Scheme. (define (triangle—area-SAS 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'angie-area-SSS 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. (define (triangle—area-SSS 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, first do that. Now write a general purpose Scheme function triangle-area that computes the area of a triangle, where the dimensions of the triangle can be specified in either of the above two ways. This function should consume four arguments. The first argument is a symbol, and will be either ’SAS or ’888. If the first 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 first 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 specified by these three arguments. Include a contract and purpose for your main function. Do not include examples or tests. (define (triangle-area 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 fiven dimensions, using SSS ;; formula if type is ’SSS, SAS f rm ai ._ p (g e is ’SAS (define (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 definitions have already been completely processed in Beginning Student: (define x ‘l 0) (define y (* x (+ x 1)» (define-struct s (x y)) (define (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 defined in lectures; and o The final 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(make-S(+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 (make-s 7 7)) (cond [(> 3 3) (S-x (make—S 7 7))] [(Odd? 3) (even? 3)] [else (make— S 7 7)]) false (d) (f 6 5 (make-s 4 3)) (cond [(> 6 5) (S-x (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 struEtufie).\ (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: (define (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 fits 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 check-expects. For each test, briefiy state its purpos i.e, which part of the function is being tested). The first one is done for you: (check-expect (mystery empty 1 —5 1) 2) ; first cond bran (check—expect (mystery empty ) "BOO!”) ; third cond branch (check—expect (mysteryfimhy 1 ) false) ; fourth cond branch, first 013] (check—expect (mystegy m y 35 1) false) ; fourth cond branch, second i Cnfiy 25 35 45) true) ; fourth cond branch, all clau (check—expect (mys ery/empty 1 0 1) (cons 1 empty)) ; first branch boundar) (check—expect (mysfiry 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: (define (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 final grade), the number of days before it is due, and the estimated number of hours to complete it. (a) (3 points) Give a structure definition for an assignment structure with fields course, weight, daysflllDue, and timeNeeded. (define-struct assignment (course weight daysTillDt/te time— Needed» (b) (5 points) Complete the following data definition for assignments: ;; An assignment = ( — — — —) make—assignment String Nat Nat Nat \ ' x (c) (4 points) Give a template for functions that consume an assggnen . (define (my—assignment—fit a. ang yer/Lt) . .. (assignment—coursew igm emf) .. . . .. (assignment-weigg ass’gnment) .. . . . . (assignmenf—dhysTil D e anassignment) . . . . .. (assignmentrtfiite eeded anassignment) .. . ) \/ (d) (4 points) Time is tight, and , ou want to make effective use of the time you have. Write the scheme function high-payofl‘? that c sumes an assignment and produces true if the assignment is worth at least 10 percent of your final grade, and can be completed in 3 hours or less. Do not include the contract, purpose, examples, or tests. (define (high—payofl? anassignment) (and (>: (assignment— weight anassignment) 10) (<: (assignment—timeNeeded anassignment) 3))) (e) (4 points) Write the function lost-cause? 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. (define (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 definition and data definition: (define-struct histogram (as bs cs)) ;; A Histogram = (make-histogram Nat Nat Nat) (a) (6 points) Write the Scheme function one-answer, 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, (one-answer (make-histogram 2 5 3) ’B) should produce (make-histogram 2 6 3). Include a contract. ;; one—answer : Histogram Symbol —> Histogram (define (one—unwer his! ans) (cond - [(symbol:? ans ’A) \ ' \ (make-hismgmm -- x (+ 1 (hismgmm-agig?! (his!0gmm— s h! !) __ (his!0gm -C [(symbol: ? (13% ’ AB (make-hism rm (h\’s!0g€'m —as his!) 1 (hismgmm—bs his!)) (hi. mgram—CS his!))] [(symbol: ? ans ’C) (make-hismgmm (his!0gmm—as his!) (his!0gmm—bs his!) (+ 1 (hismgmm-cs 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 process-answers, which consumes a list of answers, and produces a histogram of all answers received. For example, (process-answers (cons “A (cons ’ B (com ’0 (cons ’A empty))))) should produce (make-histogram 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 (define (process—answers answers) (cond [(empry? answers) (make—histogram 0 0 0)] [else (one—answer (process-answers (rest answers» (first answers))])) (check—expect @FOCBSS-anSi'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

Page1 / 13

mid1sol - University of Waterloo Midterm Examination #1 CS...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online