H211 Introduction to Computer Science (Honors) Fall 2000

Assignment 1: Getting Started

Due Sun Sep 3 11:00:00 2000

Reading

Instructions

For each assignment, you will submit a single file of Scheme code. Save your solutions for assignment one in a file named a1.ss (the .ss extension stands for ``Scheme source code'').

Put your name, e-mail address, and the assignment number in a comment at the top of your file. In Scheme, a comment starts with the character `;' (a semicolon) and continues to the end of the line. For example, my a1.ss file would begin:

     ;;; Oscar Waddell (owaddell@cs.indiana.edu)
     ;;; Lab Section 1234
     ;;; H211 Assignment 1
Feel free to include other information that would be helpful to a human reader (e.g., the grader) as comments in the file.

Handing in your solutions

When you have completed the assignment, make sure that your file a1.ss loads properly. Files that do not at least load cannot be graded and will not receive credit. Save your work on both your student locker and on a floppy disk.

When you are ready to turn in your solutions, go to the submissions page. You can hand in your assignment as many times as you want before the due date. Your last submission is the one that will be kept and graded. (Note: The handin link should now be operational!)

DrScheme users: Always save your work by selecting ``Save Definitions as Text'' from the ``File'' menu. Do not click the ``Save'' button.

Exercises

Save your solutions the these exercises in a file named a1.ss as described above.
  1. Use define to establish the following variable bindings.
    VariableValue
    zero0
    pi3.14159
    wheel27

    Test your code by evaluating the definitions (e.g., in DrScheme, click ``Execute'') and then try typing the name of each variable in the interaction window. The result should look like this:

       > zero
       0
       > pi
       3.14159
       > wheel
       27
    

  2. Translate the following arithmetic expressions into the fully parenthesized ``prefix notation'' of Scheme expressions. Establish a binding (using define) for each of the variables in the following table to the corresponding prefix expression. The first one is translated for you as an example (you will need to establish the variable binding yourself in a1.ss).

    Remember from algebra that you perform multiplication and division before addition and subtraction, and that you should perform multiplications and divisions from left to right, and likewise perform additions and subtractions from left to right.

    Do not simplify the expressions.

    VariableInfix ExpressionPrefix Expression
    a2 * (3 + 5)(* 2 (+ 3 5))
    b(5 + 4) * 3
    c(-7 / (8 + (9 * 5)))
    d(7 * 0.5) + (6 * 0.4) + (5 * 0.3)
    e(100 + 94 + 96) / 3
    f(11 - (((27 / 18) / 131) + 927))

    Check to see that your expressions evaluate to the expected values. For example, if we execute the definitions, the value bound to the variable a should be 16:

       > (define a (* 2 (+ 3 5)))
       > a
       16
    

  3. In mathematics, formulas are used to describe values. For example, the area of a circle is equal to pi times the square of the radius r. We can define a Scheme procedure that computes the area of a circle as follows.
        (define circle-area
          (lambda (r)
            (* pi (square r))))
    
        (define square
          (lambda (x)
            (* x x)))

    Note that we already established a binding for the variable pi in exercise 1. We also defined and used a helper procedure square to square the radius.

    Here's an example showing the result we obtain when we apply the procedure we've just defined:

       > (circle-area 12)
       452.38896
    

    To help us determine which is the best deal at Mama Mia's Pizza, define a procedure price-per-square-inch that takes two arguments: the diameter of the pizza and the price. Your procedure should return the price per square inch of pizza.

    Here are some examples:

       > (price-per-square-inch 10 5.99)
       0.07626711314971082
       > (price-per-square-inch 12 7.99)
       0.07064717052334787
       > (price-per-square-inch 14 10.99)
       0.07139242048953374
       > (price-per-square-inch 16 12.99)
       0.06460701428257666
    
  4. Define a procedure celsius->fahrenheit that converts from degrees celsius to degrees fahrenheit using the formula
       F = (9/5 * C) + 32
    For example, we should get the following:
       > (celsius->fahrenheit 100)
       212
       > (celsius->fahrenheit 0)
       32
    

  5. Define a procedure fahrenheit->celsius that converts degrees fahrenheit to degrees celsius. For example, we should get the following:
       > (fahrenheit->celsius 212)
       100
       > (fahrenheit->celsius 98.6)
       37.0
    

  6. Define a procedure compute-score that computes the weighted score for the course. The procedure takes four arguments: the combined score for the homeworks, the score for midterm 1, the score for midterm 2, and the score for the final exam. The weights for the various components are given below.

    ComponentWeight
    homework10%
    midterm125%
    midterm225%
    final40%

    For example, we should get the following:

       > (compute-score 70 80 90 100)
       89.5
       > (compute-score 100 92 95 87)
       91.55000000000001
    

Checklist

Check to make sure you haven't skipped any of the exercises.

Your file should contain definitions for: zero, pi, wheel, a, b, c, d, e, f, price-per-square-inch, celsius->fahrenheit, fahrenheit->celsius, and compute-score.

When you're all done, review the submission instructions.