University of Waterloo**We aren't endorsed by this school
Course
CS 115
Subject
Computer Science
Date
Jan 15, 2025
Pages
6
Uploaded by MateThunder3261
CS 115 Fall 2024 Assignment 1 Solutions Note: Though it was not required for this assignment, these solutions follow the complete Design Recipe as taught in Module 04. Students were not expected to include function purposes, contracts, examples, or tests. Nor were students expected to define helper functions, though one is included in our solution to Q4. For full style marks, students were expected to use constants, meaningful names, and make appropriate use of whitespace in their solutions. This document is for the exclusive use of kchetan
LoOo~NOOULE WN P 11 RRRRRkoRkkikkokkkekscksokokkkekskokskokskkkkkskokskokskskokekkskokskokskkokokokskokk i Lori Case (Instructor) L2 CS 115 Fall 2024 iy Assignment 1, Question 1 § 1 RRRRRRoRkkkickokokskkkoksokkokkkekskokskokkkkkkkokskokkekkekkskokskokkskokkkokok ok ;3 (bottom-of-batman x) produces the y-value for the consumed value x Y corresponding to the equation for the bottom of the Batman graph ;3 bottom-of-batman: Num —> Num ;3 Requires: -4 <= x <= 4 ;; Examples: (check-expect (bottom-of-batman (check-within (bottom-of-batman (check-within (bottom-of-batman (define (bottom-of-batman x) (+ (abs(/ x 2)) 0) -3) -4) -2.4619554199448697 0.0001) 2.99) -1.3219267293656327 0.0001) (- (x (/ (= (x 3 (sgrt 33)) 7) 112) (sqgr x))) -3 (sqrt (- 1 (sqr (- (abs (- 14 Testst (check-within (bottom-of-batman (check-within (bottom-of-batman (check-within (bottom-of-batman (check-within (bottom-of-batman (check-within (bottom-of-batman (check-within (bottom-of-batman (check-within (bottom-of-batman (check-within (bottom-of-batman (check-within (bottom-of-batman (abs x) 2)) 1)))))) 4) -2.46196 0.0001) -2.95925) -1.32137 0.0001) 2.95925) -1.32137 0.0001) -2) -2.36549 0.0001) 2) -2.36549 0.0001) -0.5) -1.90682 0.0001) 0.5) -1.90682 0.0001) -2.99) -1.3219267293656327 0.0001) pi) -1.34108 0.0001) This document is for the exclusive use of kchetan
LoOo~NOOULE WN P 51 RkkkskoksokokskskokskskoksokokekskokkkokskokskakskokskkokeKkokekskokkkoksFokokak koo KRk ok i Lori Case (Instructor) L2 CS 115 Fall 2024 iy Assignment 1, Question 2 31 kkkkkskoksskokskokokskskoksokkekokokskskoksokokskokokskskoksokkekokokkkokskskokskkokkkokFokokokok ;3 Constants (define gauge-length 10) ;3 (stitches-needed gauge size) produces the number of stitches 12 needed for a garment size cm wide, given gauge stitches per B 10 cm ;3 stitches—needed: Nat Num —> Nat ;3 Requires: ;3 % gauge > 0 3 % size >= .0 ;+ Examples: (check-expect (stitches-needed 1 0) @) (check-expect (stitches-needed 17 53) 91) (define (stitches-needed gauge size) (ceiling (% (/ gauge gauge-length) size))) 11 Tests: (check-expect (stitches-needed 1 10) 1) (check-expect (stitches-needed 9 111.7) 101) (check-expect (stitches-needed 10 14) 14) (check-expect (stitches-needed 10 20) 20) (check-expect (stitches-needed 12 50.25) 61) (check-expect (stitches-needed 18 45) 81) (check-expect (stitches-needed 20 5) 10) (check-expect (stitches-needed 20 10) 20) (check-expect (stitches-needed 20 15) 30) This document is for the exclusive use of kchetan
LoOo~NOOULE WN P 51 RkkkskoksokokskskokskskoksokokekskokkkokskokskakskokskkokeKkokekskokkkoksFokokak koo KRk ok i Lori Case (Instructor) L2 CS 115 Fall 2024 iy Assignment 1, Question 4 31 kkkkkskoksskokskokokskskoksokkekokokskskoksokokskokokskskoksokkekokokkkokskskokskkokkkokFokokokok ;3 Constants for the costs and sizes of needed supplies for our ice cream party: (define ice-cream-cost 7.50) (define container-size 2) (define scoop-size 0.125) (define scoops—per—-container (floor (/ container-size scoop-size))) (define cones-cost 2.75) (define cones-per—box 18) (define toppings—per-serving 0.25) ;3 (cost-of-item num-servings servings-per-unit cost-per—unit) produces the g total cost of buying enough units (containers or boxes) to serve B num-servings, when one unit serves servings-per-unit and each unit L) costs cost-per-unit dollars ;3 units—to-buy: Nat Nat Num —> Nat ;3 Requires: cost-per-unit >= 0@ ;; Examples: (check-expect (check-expect (cost-of-item 100 20 3) 15) (cost-of-item 100 15 10) 70) (define (cost-of-item num-servings servings-per-unit cost-per-unit) (* cost-per-unit (ceiling (/ num-servings servings-per-unit)))) ;3 (ice-cream—party-cost num-servings) produces the total cost of L buying supplies to serve num-servings ice cream cones. ;3 lce-cream-party-cost: Nat —> Num ;; Examples: (check-expect (check-expect (ice-cream-party-cost 0) 0) (ice-cream-party-cost 10) 12.75) (define (ice-cream-party-cost num-servings) (+ (cost-of-item num-servings scoops—-per—container ice-cream-cost) (cost-of-item num-servings cones-per-box cones-cost) (* num-servings toppings-per-serving))) WO Tagtsy (check-expect (check-expect (check-expect (check-expect (check-expect (check-expect (check-expect (check-expect (check-expect (check-expect (check-expect (ice-cream-party-cost 1) 10.50) (ice-cream-party-cost 5) 11.50) (ice-cream-party-cost 6) 11.75) (ice-cream-party-cost 16) 14.25) (ice-cream-party-cost 17) 22.0) (ice-cream-party-cost 18) 22.25) (ice-cream-party-cost 19) 25.25) (ice-cream—party-cost 32) 28.50) (ice-cream-party-cost 33) 36.25) (ice-cream-party-cost 100) 94) (ice-cream-party-cost 101) 94.25) This document is for the exclusive use of kchetan
59 | (check-expect (ice-cream-party-cost 1000) 876.50) 60 | (check-expect (ice-cream-party-cost 123456789) 107596027.5) This document is for the exclusive use of kchetan