CPSC120B
Fundamentals of Computer Science I

Lab 3

Operators

Practice Problem #1

Write a program that stores an integer in a variable called theta. The program should compute and print the smallest number of degrees necessary to turn left from 0° to match that angle. In order for the program to work with angles greater than 360, it should use the mod (%) to perform the computation.

Practice Problem #2

Write a program that stores an integer in a variable called square. The program should compute and print the square root of the value stored in the variable square. Note that the square root of a number can be computed by raising the value to the \(\frac{1}{2}\) power.

World's Largest Ice Cream Cone

The world's largest ice cream cone was 9 feet tall, according to the Guinness Book of World Records. While impressive based on the height alone, one has to wonder how much ice cream actually went into making such a monstrosity. Math is of course our friend, but writing a program could make it easier to figure out how big of an ice cream cone you and your friends would have to make in order to beat the current record.

Details

In Emacs, create a Python program in a file called ice_cream.py. The program should print the volume, in cubic feet, of the worlds largest ice cream cone. The cone is 9 feet tall with a radius of 2 feet. Assume that the entire cone is filled with ice cream and that there is a perfect half-sphere of ice cream on top of the cone, like this:

The volume of a cone can be computed using the equation:

$$v = \pi \cdot r^{2} \cdot \frac{h}{3}$$

Where \(v\) is the volume of a cone, \(r\) is the radius of the cone, and \(h\) is the height of the cone. The volume of a sphere can be computed using the equation:

$$v = \frac{4}{3} \cdot \pi \cdot r^{3}$$

Where \(v\) is the volume of a sphere and \(r\) is the radius of the sphere. The program should use meaningful names for values and simple statements to make the program more readable. The output should be nicely formatted and labeled.

Example

$ python3 ice_cream.py
The volume of the cone is 37.69911184307752 cubic feet.
The volume of the sphere is 33.510321638291124 cubic feet.
The volume of ice cream is 54.454272662223076 cubic feet.

Hint

  1. First, create variables for the input values to the program. In this case cone_height and cone_radius are the inputs to the program. Set the height variable to 9 and the radius variable to 2.
  2. Next, create variables for the computations that will be performed. In this case cone_volume, sphere_volume, and ice_cream_volume. Set each of these variables equal to arithmetic expressions that compute the correct value. Note that the ice_cream_volume variable depends on the other two variables, so it must be defined after them.
  3. Finally, create print statements that print the calculated values nicely formatted.

Challenge

So how many children can the world's largest ice cream cone feed? Modify your program to calculate and print the result. Assume that a single serving of ice cream is 4 oz and note that there are 957.506 oz in a cubic foot.

Finding Your Pace

You might think that running has very little to do with Computer Science. However, there are a lot of useful computations we can do to help a runner figure out how fast they ran. Race distances are typically given in kilometers: 5k, 10k, etc. A fast runner might run a 5K in 20 minutes, but the winning time is usually closer to 15 minutes. In the US, runners typically measure their pace in minutes per mile. We will write a program to perform these computations.

Details

In Emacs, create a Python program in a file called runner.py. Your program should have variables that store:

Your program should then compute and print the user's pace in minutes per mile. Make sure you use proper variable names, format the output nicely, and follow the courses code conventions.

Example

$ python3 runner.py
If you ran a 5 kilometer race in 27 minutes and 15 seconds,
your pace will be 8.790322580645162 minutes per mile.

Hint

  • Begin by defining the variables and constants. You will need variables for the distance in kilometers, distance in miles, minutes part of the time, seconds part of the time, and minutes per mile.
  • Then, compute the distance in miles and the time in minutes. Use the approximation that 5 kilometers is about 3.1 miles.
  • The pace of the runner is just their time divided by the distance of the race in miles.

Challenge

Many runners set goals to improve their pace. Add to your program a variable for the user to store their desired minutes per mile into. Compute how quickly they would have to run the race, and print this value as well.

Submission

Please show your source code and run your programs for the instructor or lab assistant. Only a programs that have perfect style and flawless functionality will be accepted as complete.