CS8, 10F, H04, due Thu Lecture 10.07—Miller/Ranum 2.6-2.7 (thru p. 62-80).—Total points: ?

Available online as http://www.cs.ucsb.edu/~pconrad/cs8/10F/homework/H04—printable PDF

Name: (4 pts)   Umail Address: (4 pts)   @umail.ucsb.edu
Lab Section (2 pts)—circle one:  9am   10am   11am   noon   unknown   crashing 
You may collaborate with at most ONE other person on this homework assignment. If you do, please enter his/her name here:  
  (He/she should also enter your name on her/his assignment.)

(Note: For now, circle the lab section you are registered for on GOLD. If you need to request attendance at a different lab section because of an ACTUAL SCHEDULE CONFLICT, please email pconrad@cs.ucsb.edu with details)

This assignment is due in Lecture on Thursday, 10.07.
It may ONLY be submitted in Lecture, in NH1006 at 2pm on Thursday.
You must come IN PERSON to turn it in during your assigned Lecture section.

Late Policy: No email submission allowed—and don't "slip it under my door". If you need to make it up, you must do so during office hours, or make an appointment to see me, and you must request this appointment within 48 hours of when the assignment was originally due.

Personal Day/Sick Day policy: Everyone is permitted one "personal day/sick day" when you get to make up a missed homework assignment for free during office hours or via appointment. After that, you may not make up the homework assignment—you can only earn back the points through extra credit opportunities.

(For more details, see the syllabus and the homework policy)

Reading: For H03, you were supposed to have read Chapter 2, p. 45-61 (Sections 2.1-2.5). If you didn't read those yet, read them now. Then read the rest of Chapter 2, pages 62-80 (Sections 2.6 through 2.7) Then, answer the following questions. Be sure to check both sides.

  1. (3 pts) What is meant by a "Monte Carlo" approach to solving a problem?

  2. (3 pts) How can you approximate the value of pi by "throwing darts"? Explain this the way you would explain it to a friend or a roommate. You can assume that your friend understands what the circumference and area of a circle are, and how to compute the area of a square.

  3. (4 pts) One of the assumptions of the Monte Carlo simulation for computing pi is that the place the dart lands is truly random inside the square or circle—uniformly distributed over the space. (In the computer simulation, this is a reasonable assumption.)

    If you were to throw darts in real life in order to compute pi, what would be the problem with that assumption of true randomness—and how would it affect the accuracy of your computation? That is, would your estimate of pi be too high, or too low? Justify your answer.

Please turn over for more...

...continued from other side

  1. (10 pts) (problem 2.3 from page 81 in the textbook, with typo corrected)

    Write the definition of a function to compute the area of a circle with a radius of r.
    Use r as the parameter to the function. Use the value of π from the math module.

    (Note that this corrects a typo in the version of the problem in the book, which suggested using n as the parameter. That, of course, makes no sense.)

  1. Section 2.6.1 and 2.6.2 discuss Boolean Expressions, Compound Boolean Expressions, and Logical Operators

    1. (2 pts) A Boolean expression can have one of two values—what are these values?

      (For full credit: use the correct case—uppercase vs. lowercase letters, in the right spots!)

    2. (2 pts) What is the relational operator that tests whether one quantity is equal to another quantity?

    3. (2 pts) What value will the expression 5 < 7 evaluate to? (Be careful about case!)

    4. (2 pts) What value will the expression (2 + 3) == 6 evaluate to?

    5. (2 pts) Suppose you have variables x and y.

      How would you write a boolean expression that is true when both x and y are greater than zero?

    6. (2 pts) Suppose you have variables a, b and c.

      How would you write a boolean expression that is true only when b2 - 4ac is not less than 0? (Be sure to note the difference between math notation and Python notation!)

  1. (3 pts) In formal logic, the negation of a boolean expression e (written not e in Python) is one where

    not e is always True when e is False
    not e is always False when e is True.

    Sometimes in informal language, we say "the opposite of e" instead of the "negation" of e.

    Consider this question:

    "What is the opposite of (or the negation of) x > 5"

    If you answer x < 5, you are wrong.

    The correct answer is x <= 5

    Why is x < 5 wrong, and x <= 5 correct?
    (Explain briefly)

  2. (5 pts) Section 2.6.3 describes selection statements—statements that make a choice between two different outcomes (or among several possible outcomes.)

    The selection statements described are the if/else and the if/elif/.../elif/else

    Write an if statement that checks whether a variable called balance is greater than or equal to zero, or less than zero, and prints an appropriate message.

    If it is greater than or equal to zero, the statement should print "ok".

    But if it is less than zero, print the word "overdrawn".

End of H04