CS115M1S2009

CS115M1S2009 -...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS115/116/135/136/2**/3**招生,價格面議,經驗豐富,童叟無欺,請認准刁一勾牌 private tutor TEL: 519-729-1582 MSN: odaeagle@hotmail.com 以下廣告欄招租。。哈哈 UWaterloo CS115 Midterm_1 2009 Summer 2009 Oda_Eagle Production CS115/116/125/134/135/136/241/240/341/343 Tutor odaeagle@hotmail.com The following notes are relevant for all questions: • We use the term “Scheme” as a short form for “Beginning Student Scheme with List Abbreviations” • Unless we ask specifically for another part of the design recipe, the instructions “Create a Scheme function” means that you are to give the definition only. • We use arithmetic functions as well as following built-in functions, described briefly here for your reference: ◦ max determines the largest of two or more numbers ◦ min determines the smallest of two or more numbers ◦ sqrt determines the positive square root of a number ◦ string-length determines the length of a string ◦ string-append combines two or more strings into a single string, in the order given ◦ check-expect determines if two values are exactly the same, and is to be used for tests when they are requested CS115/116/135/136/2**/3**招生,價格面議,經驗豐富,童叟無欺,請認准刁一勾牌 private tutor TEL: 519-729-1582 MSN: odaeagle@hotmail.com 以下廣告欄招租。。哈哈 Question 1. [ 10 marks ] Scheme Errors for each row in the table below, determine what would happen if you opened DrScheme and tried to evaluate the given code. If there is an error, briefly explain how Scheme's Syntax rules are violated or describe what goes wrong when the code is run. If there is no error, give the value produced by DrScheme. The first two rows have been completed for you. Scheme Code Explanation/Value (+12) 3 (+12)) Error. There is an extra ). ( * 12 3 ) ( -4_) ( + “Hello” “world” ) ( min ( remainder 5 2 ) 2 ) ( + ( * ( - ( max 1 1 1 ) 1 ) 1 ) 1 ) ( substring “hi” 1 3 ) ( or -2 1 0 ) ( / 15 ( - 5 5 ) ) ( define f ( + a 3 ) ) ( define x ( min 4 6 ) ) ( define y ( max 5 6 ) ) ( cond [ ( > x y ) ( - x y ) ] ) CS115/116/135/136/2**/3**招生,價格面議,經驗豐富,童叟無欺,請認准刁一勾牌 private tutor TEL: 519-729-1582 MSN: odaeagle@hotmail.com 以下廣告欄招租。。哈哈 2 ( 6 Marks ) Scheme Function Definition Write the Scheme definition of the function f( x , y ) = x2+y2-2xy. for this question, you do not need to follow the steps of the design recipe - only the function definition required. 3 ( 6 Marks ) Tracing Trace the following Scheme program: ( - ( - 6 1 ) ( + ( min 2 3 ) ( * 3 3 ) ) ). Put one step on each line. You may not need all the lines. CS115/116/135/136/2**/3**招生,價格面議,經驗豐富,童叟無欺,請認准刁一勾牌 private tutor TEL: 519-729-1582 MSN: odaeagle@hotmail.com 以下廣告欄招租。。哈哈 Question 4. [ 8 marks ] Partial Design Recipe Suppose you wish to write a Scheme predicate differ-by-one? That consumes two strings of the same length and produces true if they differ by only one position when compared character by character, and produces false otherwise. In the spaces below, write the contract, purpose, one example and header for the predicate differ-by-one? Do not write the function body. Contract: Purpose: Example: Header: 5 ( 4 marks ) Testing The function convert-grade consumes an integer for grade, and produces a string grade in A,B,C,D,E, and F. (define ( convert-grade g ) ( cond [ (>= g 90 ) “A” ] [ (>= g 80 ) “B” ] [ (>= g 70 ) “C” ] [ (>= g 60 ) “D” ] [ (>= g 50 ) “E” ] [else “F” ] ) ) Write two tests for convert-grade. Note, the two tests have to test different branches of this conditional expression. CS115/116/135/136/2**/3**招生,價格面議,經驗豐富,童叟無欺,請認准刁一勾牌 private tutor TEL: 519-729-1582 MSN: odaeagle@hotmail.com 以下廣告欄招租。。哈哈 6. ( 6 Points ) Function Evaluationven function definition, evaluate each function application. (a) uw-userid ( define ( uwdir-id firstname lastname ) ( truncate ( string-append ( substring firstname 0 1 ) lastname ) ) ) ( define ( truncate str ) ( substring str 0 ( min ( string-length str ) 8 ) ) ) Function application Result ( uw-userid “Sarah” “Alwin” ) (uw-userid “Anthony” “William”) (uw-userid “Eagle” “Diao” ) (b) which-path ( define ( which-path nbr ) ( cond [ ( or ( < nbr 10 ) ( = nbr 15 ) ( > nbr 20 ) ) “path 1” ] [ ( and ( > nbr 13 ) ( < nbr 19 ) ) “path 2” ] [ else “path 3” ] ) ) Function application (which-path 8 ) (which-path 14 ) (which-path 15 ) Result CS115/116/135/136/2**/3**招生,價格面議,經驗豐富,童叟無欺,請認准刁一勾牌 private tutor TEL: 519-729-1582 MSN: odaeagle@hotmail.com 以下廣告欄招租。。哈哈 7. ( 10 Marks ) Complete Design Recipe Rogers high-speed Internet charges 50 dollars per month, which allows a download and upload limit of 60 GB in total. For each additional gigabyte of usage, Rogers charges 2 dollars, to a maximum of 25 dollars per month. Using all the steps of the design recipe, create a Scheme function Internet-bill that consumes an integer for the Internet usage in gigabytes and produces the cost for the month. All of the steps of the design recipe are important: only a small number of marks will be allotted to the correctness of the definition of Internet-bill. In addition, you should define any useful constants. ...
View Full Document

Ask a homework question - tutors are online