HW3 + Solutions

posted Sep 13, 2012, 5:40 PM by Samuel Konstantinovich   [ updated Oct 8, 2012, 10:30 PM ]
SOLUTIONS are posted as a comment. Click this post to see comments.

This assignment has two parts, one should be submitted as an RKT file on the homework server. The other should be printed and brought to class.
PART 1:
Submit on the homework server: (Due 9/16/2012 11:59pm)
(If your name is not in the homework server, you should email me to let me know ASAP)
Write the following functions (definitions only):
Hint: You may want to define a square function before defining these three functions
1. (AreaRectangle a b) that takes two arguments a and b and returns the area of a rectangle whose length is a and whose width is b.
2.  () that takes two arguments a and b, and returns the sum of a squared and b squared. 
3.  (areacircle r) which takes an argument r, and returns the area of a circle of radius r. π is already defined for you by typing the predetermined variable pi. If not, then write the following variable definition: (define pi 3.14159)

PART 2:
Typed and printed: (Due 9/19/2012 in class)
1. Convert the infix expression to prefix.
 a.   ( 5 - 4 * 2 ) / ( 10 - 5 ) + 3
 b.   ( 2 +  3 ( 4 - 2) / ( 1 - 3 / 5 ) ) *EDITED: closed parenthesis at end* 
2. Evaluate the expressions (show the steps you took line by line) You may verify your result using DrScheme. Parts f and g require you to refer back to Part1 of the assignment. 
 a. ( * (+ 4 7 9) (/ 15 5) (- 3 -1) )
 b. (/ (- 20 -4)   (* -3 2 -1))

    (define a 4)
    (define b 5)
    (define c  (+ a b -1) )
 c. ( - (+ a (* -1 c) b) (* a b))
 d. (+ (quotient c b) (remainder a 4)) 
 e. (+ (* a b -1) (- c (* b 2)))
 f. (AreaRectangle 12 9)
 g. (sum-of-squares 4 7)



Solutions:
FUNCTIONS: (posted 9/26)
1. 
(define (AreaRectangle a b) (* a b) )
2. 
(define (sum-of-squares a b) (+ (* a a)(* b b)))
3.
(define (areacircle r) (* pi r r))
OR
(define (areacircle r) (* 3.14159 r r))


WRITTEN WORK: 
1a. The +3 happens AFTER the division:
(+ (/ (- 5 (* 4 2))(- 10 5)) 3) OR [for making the +3 more obvious] (+ 3 (/ (- 5 (* 4 2))(- 10 5)) )

1b. The 2 is added after the multiplications and divisions. 3(4-2) is a multiplication of 3 and parenthesis.
(+ 2 (/ (* 3 (- 4 2)) (- 1 (/ 3 5))))

2
a. 240
b. 4
c. -19
d. 1
e. -22
f. 108
g. 65
Comments