F09-CS135-mt1

F09-CS135-mt1 - Warning The content of CS135 changes from...

Info iconThis preview shows pages 1–12. 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
This is the end of the preview. Sign up to access the rest of the 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 specified, you must include the contract, examples, and function definition portions of — Helper functions must also include a purpose. — Examples must use check-expect, and unless otherwise allowed, must be different from any examples supplied in the question. 0 You do not need to write out data definitions or templates unless we specifically 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 built-in function discussed in class, unless specifically 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 definitions 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 defined. (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 num-disn‘ncr-reai-roots(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 first 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 helfifufigms, 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 check-expect, 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 finish 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 Sci-E'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 functionsfl,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 . ' [elsefizise ) < > [(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 doubts-product? that consumes four numbers and produces true if and only if the numbers form a double product. For example, 0 (double-product? 2 3 6 4) :)> true because 2 X G = 3 X 4 o (double-product? 6 10 3 5) :> true because 6 X 5 = 3 X 10 o (double-product? 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 definitions have been processed in the Beginning Student language: (define r 6) (define s (* r 2)) (define (f s p) (+ p r s» (define (g x r) (cond [(and (> r 8) x) x] [else r])) show the step-by-step simplification of each of the following expressions. A simplification 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 simplification 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.

Page1 / 12

F09-CS135-mt1 - Warning The content of CS135 changes from...

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

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