CM151: Lab 3 -- Working with Math.


Lab Goal:

Pratice working with the Math Module and more complex numeric computations.


Assignment:

    • Write a function that determines the distance to a lightning strike based on the time elapsed (input to the function) between the flash and the sound of thunder.

    • The speed of sound is approximately 1100 ft/sec and 1 mile is 5280 feet.

    • The function should be in a program that prompts the user for the elapsed time (in seconds), calls the function and prints the result of calling the function, i.e., the program will pass a value to the function, the function will return a result, and the result is not printed inside the function.

    • We've talked about defining functions and returning values in class. If this doesn't ring a bell, check out the book (page 181 might help) for more about functions, or check out the function fib2 here.


    • Write a function that calculates the area of a triangle given the length of the 3 sides a, b, and c using these formulas:


    • Recall that the square root function in python is called sqrt and is part of the math module.

    • You should write two functions to solve this problem:
      • one to calculate s, and
      • another to calculate the area once s has been calculated.

    • The main program should prompt the user for the length of the 3 sides, call the arithmetic functions, and print the results.

    • It's best if you have a third function that prompts the user for the length of a side and have the main program call that function 3 times (once for each side).



    • Write a program (again composed of several functions) to determine the length of a ladder required to reach a given height when leaned against a house.

    • The height and angle of the ladder (in radians) should be obtained from the user.

    • Use this formula:



    • The sine function in python is called sin and is part of the math module.





    • Extra question: Optional and will not affect grade

    • Write a function that will take a positive integer, n, as an input parameter. It will return the sum of the numbers from 1 to n.

    • However, you cannot use any loops. Instead, the function you write should call itself. This is called recursion. The trick is that you want to make sure that the function calls itself the right number of times, and doesn't go on forever.

    • Some people find the idea of recursion straightforward, and others do not. If you're in the latter category, no worries. Matt will discuss it in class after the lab, but you won't be responsible for the concept.


Submission

Once you have finished your three programs, upload the three .py files to Moodle.