CS115_Winter_2009_M1

CS115_Winter_2009_M1 - CS115/116/135/136/2**/3** Private...

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 Tutorial Services MSN: odaeagle@hotmail.com E-mail: eaglediao@gmail.com Please Email me for more information such as price and available time UWaterloo CS115 Midterm_1 2009 Winter 2009 Oda_Eagle Production CS115/116/125/134/135/136/241/240/341/343 Tutor MSN: odaeagle@hotmail.com E-mail: eaglediao@gmail.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 Tutorial Services MSN: odaeagle@hotmail.com E-mail: eaglediao@gmail.com Please Email me for more information such as price and available time Question 1. [ 7 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 ). (+111) -5 (3*4) ( sqrt ( + 25 ( / 5 0 ) ) ) ( string-length 9 ) ( - ( + ( max 3 4 ) ( min 1 2 ) ) 1 ) ( define ( f ) 42 ) Question 2. [ 5 marks ] A Basic Scheme Function CS115/116/135/136/2**/3** Private Tutorial Services MSN: odaeagle@hotmail.com E-mail: eaglediao@gmail.com Please Email me for more information such as price and available time Write the Scheme definition of the function g( x , y ) = ( x + y )2. for this question, you do not need to follow the steps of the design recipe - only the function definition required. Question 3. [ 5 marks ] Tracing Trace the following Scheme program: ( + ( - ( * 2 5 ) ( min 5 2 ) ) 2 ). Put one step on each line. You may not need all the lines. Question 4. [ 8 marks ] Partial Design Recipe CS115/116/135/136/2**/3** Private Tutorial Services MSN: odaeagle@hotmail.com E-mail: eaglediao@gmail.com Please Email me for more information such as price and available time Suppose you wish to write a Scheme predicate mixed-chars? That consumes a string and produces true if that string contains both alphabetical ( a,B,c etc. ) and numerical characters ( 1,2,3 etc ), and produces false otherwise. In the spaces below, write the contract, purpose, one example and header for the predicate mixed-chars? Do not write the function body. Contract: Purpose: Example: Header: Questions 5. [ 2 marks ] Testing The function create-userid produces a string corresponding to a new userid derived from an individual's first and last names. ( define ( create-userid first-name last-name ) ( string-append first-name “.” last-name “@gmail.com” ) ) write a complete test for create-userid. Question 6. [ 3 marks ] Tracing a Conditional Expression CS115/116/135/136/2**/3** Private Tutorial Services MSN: odaeagle@hotmail.com E-mail: eaglediao@gmail.com Please Email me for more information such as price and available time Answer the following questions about the function choose. ( define ( choose num ) ( cond [ ( and ( <= 10 num ) ( <= num 19 ) ) “path 1” ] [ ( or ( < num 0 ) ( > num 15 ) ) “path 2”] [ else “path 3” ] ) ) What value is produced by ( choose 18 )? What value is produced by ( choose 12 )? Give a single value for n in ( choose n ) which produces the value “path 3” Question 7. [ 10 marks ] A complete Scheme Function with Design Recipe CS115/116/135/136/2**/3** Private Tutorial Services MSN: odaeagle@hotmail.com E-mail: eaglediao@gmail.com Please Email me for more information such as price and available time Using all the steps of the design recipe. Create a Scheme function engraving-cost that consumes a positive number per-char-cost and a non-empty string message, and produces the cost of engraving message on a MP3 player. The total cost for engraving is the number of characters( including spaces ) in message times the per-char-cost, plus an engraving fee of $4.95. All of the steps of the design recipe are important: only a small number of marks will be alloted to the correctness of the definition of engraving cost. In addition, you should define any useful constants. ...
View Full Document

Ask a homework question - tutors are online