Strict Standards: Declaration of Doku_Renderer_metadata::table_open() should be compatible with Doku_Renderer::table_open(\$maxcols = NULL, \$numrows = NULL, \$pos = NULL) in /home2/cp-wiki/htdocs/inc/parser/metadata.php on line 480

Strict Standards: Declaration of Doku_Renderer_metadata::table_close() should be compatible with Doku_Renderer::table_close(\$pos = NULL) in /home2/cp-wiki/htdocs/inc/parser/metadata.php on line 480

# CS 190C, Spring 2008: Problem Set 1

Posted: Friday, January 11, 2008

Due: Thursday, January 17, 2008, 10pm (electronic submission via Blackboard)

You have the following choice: Complete problems 1, 2, 3, and 4. Or, complete problems 2, 4, and 5.

Each program should be in a separate file. Name your files problem1.py through problem5.py. This is a good template for your programs for this problem set.

```#Aaron Lint
#Problem Set 1, Problem 1

def main():
#This is where your code should go
print "Hello world."

main()```

## Problem 1

During a thunderstorm when lightning strikes, you often hear the thunder some time after you see the flash. However, if you know the speed of light and sound, you can figure out how far away the lightning strike was based upon the amount of time you heard the bang after you heard the flash. For our purposes, since light is so much faster than sound (especially over a distance of a few miles), we assume that the light is visible the instant of the lightning strike. Thus, if we know the speed of sound, we can calculate the distance to the strike. Your program should do the following:

• Input: Delay in seconds and a temperature in Fahrenheit
• Output: Distance to the lightning strike in miles (e.g., “The lightning strike was X miles away.”)

In dry air, the speed of sound S in meters per second is approximately

`S = 331.3 + 0.606*T, where T is given in degrees Celsius.`

Hint: Make sure to do the necessary conversions. You are given the temperature in Fahrenheit, and need to use it in Celsius. You use the speed of sound in meters per second to find out how many meters were traveled by the sound. The final answer should be in miles.

## Problem 2

A ball is dropped from the roof of a tall building. Assume that the ball starts at rest, and that the drop is exactly the height of the building. Given the amount of time the ball takes to contact the ground, calculate how high the building is.

• Input: Time to drop ball from roof to ground in seconds
• Output: Height of the building in feet (e.g., “The building is X feet high.”)

Assume that standard gravity is g = 9.8 m/s^2 and that all other factors are negligible.

Recall the formula for distance of a fall, based on acceleration: D = 0.5 * g * t^2. Be careful about the conversion from feet to meters.

## Problem 3

The user inputs the month (m), day (d), and year (y) of a date by being prompted with appropriate text. The output of your program is the day of the week for that date (w). For m, let 1=January, 2=February, etc. The year should be given with four digits (i.e., 2008, not 08).

Use the following formulas to compute w:

```y0 = y - floor((14 - m) / 12)
x = y0 + floor(y0 / 4) - floor(y0 / 100) + floor(y0 / 400)
m0 = m + 12 × floor((14 - m) / 12) - 2
w = (d + x + floor((31 × m0) / 12)) % 7```

where 0=Sunday, 1=Monday, 2=Tuesday, and so on.

Note: When converting the formulas into Python code, consider the right number format. It is not necessary to use math.floor from the math library.

## Problem 4

Section 2.7 of Zelle (pages 42-45) develops and presents program futval.py, which computes the value of an investment. This problem extends the investment program so that (i) the number of years is a variable determined by the input and (ii) an additional yearly investment is made (it is the same for all years).

The program should prompt the user for four input values, each given on a separate line with an appropriate text prompt:

• The starting `principal` - the amount in the account before any interest is added
• The annual percentage `rate` (APR) - the percentage interest earned each year
• The `yearly` investment - the amount added to the account each year after interest is earned
• The number of `years` - the number of years to do the computation

If amount in the account is `amount`, then each year the new amount will be `amount*(1+rate)+yearly`. Your program should output amount in the account after the specified number of years (before figuring in interest and yearly investment for the next year).

## Problem 5

There exist a number of approximations of the value of pi. Two are listed below: the Wallis formula , and the Leibniz formula. Your program should approximate pi with the first k steps of the both approximations where k is given as input by the user.

Leibniz:

`pi = 4/1 - 4/3 + 4/5 - 4/7 + 4/9 ...`

Output your approximations of pi, and also print out how close each approximation is to the numerical constant math.pi.

cs190c/problemset1.txt · Last modified: 2008/01/11 15:46 by jtk