; CS101, Spring 2013 ; HW 1 ; Writing expressions. ; This file is intended to be opened within the DrRacket ; programming environment. It contains meta-data that is ; unreadable in browsers, word processors and text editors. ; Once this file is open within DrRacket: ; ; UNDER Language MENU, SELECT: ; Choose Language... Advanced Student ; Press the Run icon (display "\n CS101 Assignment 1, Spring 2013") (display "\n SOLUTIONS\n\n") ; Try pressing the Run button at the top right of the screen ; and check out what prints in the interactions window. ; PLEASE READ CAREFULLY AND SAVE OFTEN (Save button top left, ; only appears if you make changes)! Make sure none of your lines ; extend past column 80. ; (display "Problem 1. Writing algebraic Racket expressions. \n\n") ; ; Convert each of the following algebraic formulas to an ; equivalent Racket function call. Use the normal math ; precedence rules when writing these expressions. ; ; Example 1: 4 - 5 * 2 would be written (- 4 (* 5 2)) ; ; Show the expression followed by its resultant output. ; ; For the expression given in Example 1, you would type ; the expression shown below in the definitions window ; and then press Run: ; ; (printf "4 - 5 * 2 = (- 4 (* 5 2)) ==> ~a~%" ; (- 4 (* 5 2))) ; ; This will display the given string followed by an arrow ; and then the result of evaluating the expression. ; ; a. 7 + 30 * 4 ; ; b. 20 * 8 - 5 ; ; c. (1 + 5) * 6 - 12/3 ; ; Be sure to write the printf statements so that 1 blank line ; vertically separates each printf expression. You can use ; the (newline) function call OR embedded ~%s. ; ; INFORMATION: the printf function is like the display ; function, in that it causes its string argument to print ; in the interactions window. But the printf can also embed ; the string representation of evaluated expressions inside ; its string argument. The ~a says, "insert value here", and ; the evaluated result of the expression (- 4 (* 5 2)) is ; converted to a string and inserted in place of the ~a. ; The ~% causes a newline character to be printed. ; ; The printf statements for parts a-c are started for you, ; just uncomment them and fill in the missing parts. ; (printf "Example: 4 - 5 * 2 = (- 4 (* 5 2)) ==> ~a~%~%" (- 4 (* 5 2))) (newline) (newline) (printf "a. 7 + 30 * 4 = (+ 7 (* 30 4)) ==> ~a~%~%" (+ 7 (* 30 4))) (newline) (printf "b. 20 * 8 - 5 = (- (* 20 8) 5) ==> ~a~%~%" (- (* 20 8) 5)) (newline) (printf "c. (1 + 5) * 6 - 12/3 = (- (* (+ 1 5) 6) (/ 12 3)) ==> ~a~%~%" (- (* (+ 1 5) 6) (/ 12 3))) (newline) (newline) (display "Problem 2. Write an arithmetic expression to calculate John's income tax.\n\n") ; ; The government computes income tax as 17% of a taxpayer's income after ; deducting $6000. John worked at two jobs last year, earning $24718 at ; the first and $14284 at the second. Write a Racket algebraic expression ; to calculate John's income tax. ; ; It's OK if the final expression is a fraction. You can change the ; language to "Full Swindle" and run the program to see it as a decimal ; number (just change it back to "Advanced Student" afterward). ; (printf "John's income tax = (* (- (+ 24718 14284) 6000) .17) ==> ~a~%~%" (exact->inexact (* (- (+ 24718.0 14284.0) 6000.0) .17))) (newline) (newline) (display "Problem 3. Write an arithmetic expression for a carpet seller.\n\n") ; ; A carpet store computes the cost of installing wall-to-wall carpet in ; a rectangular room by multiplying the length and width of the room (in ; feet) by the cost of carpet in dollars per square foot, and adding a ; flat $50 service charge. ; ; Write this expression for a room of length 25, width 20 and a carpet ; cost of $1.75/sq ft. Embed this expression in a print statement as you ; did for problems 1 and 2. There is no need to have 2 decimals after ; the dollar amount. ; (printf "The cost of carpet = (+ (* 25 20 1.75) 50) ==> $~a~%~%" (+ (* 25 20 1.75) 50)) (newline) (newline) (display "Problem 4. Write an arithmetic expression to compute a grade.\n\n") ; ; A student writes two midterm exams and a final exam. Suppose the two ; midterms are each worth 25% of the grade and the final exam is worth ; 50%. ; ; Write an expression to calculate the final grade for a student who ; got an 85/100 on midterm 1, a 90/100 on midterm 2, and a 95/100 on ; the final. ; ; It's OK if the final expression is a fraction. You can change the ; language to "Full Swindle" to see it as a decimal number (just change ; it back to "Advanced Student" afterward). ; (printf "The final grade = (+ (* 50 (/ (+ 85 90) 200) (* 50 (/ 95 100)) ==> ~a" (exact->inexact (+ (* 50.0 (/ (+ 85 90) 200.0)) (* 50.0 (/ 95 100.0)))))