; CS101, Spring 2013 ; HW 1 ; Writing arithmetic expressions and using display, printf, ; and newline. ; 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 PLEASE WRITE YOUR NAME HERE\n\n") ; Try pressing the Run button at the top right of the screen ; and check out what prints in the interactions window. It ; should initially be only the headers of four problems and ; one example for problem 1. ; 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. ; ; You should write similar prefix expressions (already ; started for you in print statements below) inside ; printf statements for the following infix expressions: ; ; a. 7 + 30 * 4 ; ; b. 20 * 8 - 5 ; ; c. (1 + 5) * 6 - 12/3 ; ; The printf statements started for you below use the ; (newline) function to leave 1 blank line of separation ; between each printf expression. The string argument to ; each printf expression also contains a ~% to add a ; return to the end of the printed string. ; ; 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 these statements and the lines in between ; and fill in the missing parts, indicated by ... ; (printf "Example: 4 - 5 * 2 = (- 4 (* 5 2)) ==> ~a~%~%" (- 4 (* 5 2))) ;; Parts a - c should look like this (newline) ;(printf "a. 7 + 30 * 4 = ... ==> ~a~%~%" ...) ;(newline) ;(printf "b. 20 * 8 - 5 = ... ==> ~a~%~%" ...) ;(newline) ;(printf "c. (1 + 5) * 6 - 12/3 = ... ==> ~a~%~%" ...) (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" to see it as a decimal number (just change it ; back to "Advanced Student" afterward). ; ;(printf "John's income tax = ==> $~a~%~%" ...) (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 printf 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 = ==> $~a~%~%"...) (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 = ==> ~a~%"...)