advertisement

15-122 Homework 1 Page 1 of 3 15-112: Introduction to Programming and Computer Science, Fall 2013 Homework 1 Programming: Input, Output, Expressions, and Variables Due: Tuesday, September 3, 2013 by 23:59 Welcome to 15-112! This programming homework is designed to get you more practise with reading values from the keyboard, using expressions to process these value and showing results on the screen. Your submission will be made through the web interface of Autolab. To do so, please create a zipped file for all the files that you will be submitting (zip). To create a zip file, selected all the files then right click, select ’send to’ option and then click on compressed (zipped) folder. Submit the newly created zipped file at: https://autolab.cs.cmu.edu/15112q-f13 1 Fahrenheit To Celcius Conversion Task 1 (3 pts) Write a program that reads in a number representing temperature in Fahrenheit . Your program should convert Fahrenheit degrees into celcius and print the result. Make sure you save this program in a file called fahrenheitToCelcius.py (Use the exact same name with the appropriate alphabets capitalized!). If the user enters 45.5 for Fahrenheit , your result should be printed in the following format (make sure your code follows this format exactly): “ 45.5 degree Fahrenheit is 7.5 Celcius” 2 Digitization Task 2 (4 pts) Write a program that reads an integer from the user and prints the sum of it’s units, tens, and hundreds digits. For example, if the number entered by the user is 256, your program should print the 13. If the number entered by the user is 37, your program should print 10. If the number entered by the user is 4, your program should print 4. Write this program in a file called “sumofDigits.py”. The output of your program should be exactly like the following format: “The sum of digits for 37 is 10” 3 Whose Line Is It Anyway? Task 3 (3 pts) Write a program that will print the equation of a line, given two points on the line. Write this program in a file called “EquationOfaLine.py”. The equation of a line is 15-122 Homework 1 Page 2 of 3 written as: y = mx + b where m is the slope of the line and b is its y-intercept. To get two points on the line (x1 ,y1) and (x2 ,y2), read four values from the user. You can assume that all points entered by the user will always be integers. If the user enters 2 for x1, 3 for y1, 6 for x2 and 4 for y2, your program should print: “The equation of the line is y = 0.25x + 2.5” 4 Party by the pool Pool balls are arranged in rows where the first row contains 1 pool ball and each row contains 1 more pool ball than the previous row. Thus, if there are 3 rows, then we’d have 6 total pool balls (1+2+3). Task 4 (4 pts) Write a Program that reads the number of rows as an integer from the user and prints the number of pool balls in that number of rows. Write this program in a file called “PoolBalls.py”.For example, if the user enters 4, your program should print “Number of balls in 4 rows is 10”. Do not use loops. 5 Trig of Trade In mathematics, the trigonometric functions (also called circular functions) are functions of an angle. They are used to relate the angles of a triangle to the lengths of the sides of a triangle. Trigonometric functions are important in the study of triangles and modeling periodic phenomena, among many other applications. The most familiar trigonometric functions are the sine, cosine, and tangent. (Source - Wikipedia) We have all studied these functions - Sine, Cosine and Tangent in grade school. Before the use of computers became widespread, the values of these functions were determined by looking them up in tables. Modern computers use a variety of techniques to calculate these values. One simple way of estimating these functions is to use something called Taylor series. Without going into the details of how and why this series works, the following equations are used for estimating the trigonometric functions. sin(x) = x − x3 3! + x5 5! − x7 7! + ... cos(x) = 1 − x2 2! + x4 4! − x6 6! + ... tan(x) = x + 13 x3 + 2 5 x 15 + 17 7 x 315 + ... Task 5 (6 pts) For this task, write a program that asks the user to enter a value for angle. (this is the angle in degrees for which you will calculate the trigonometric values). Write this program in a file called “Trig.py”.The above equations require the angle to be in radians. To convert the user input to radians, use the following equation: π radians = degrees ∗ 180 15-122 Homework 1 Page 3 of 3 Calculate the Sine, Cosine and Tangent for this angle in radians by using the appropriate equations given above. Although this series consists of infinite terms, you will only use the first 4 terms as shown in the equations above. Print these values for Sine, Cosine and Tangent. Use the same value of x to calculate Sine, Cosine and Tangent but this time using the math library in Python - math.sin(x), math.cos(x), and math.tan(x) (Don’t forget to import the math library). Now find and print the percentage error between your calculation and those obtained by the Python library. Let’s assume that “mysinecalc” is the value of sin(x) calculated by you using the equations defined earlier and “libsinecalc” is the value of sin(x) calculated by Python library. The percentage error would be: (mysinecal - libsinecalc)/libsinecalc To summarize, you will have to implement the following steps for this question. 1. Read value for angle from the user in degrees 2. Change the angle to radians 3. Estimate the Sine of this angle by using Taylor series. 4. Print the estimated value of Sine. 5. Calculate the Sine of the same angle by using math.sin( ) function. 6. Print the calculated value of Sine. 7. Calculate and print the percentage of error between the two values of Sine obtained in steps 3 and 5 8. Repeat steps 3-7 for Cosine and Tangent values The output of your program should follow the following format (Notice that most numbers in this sample are bogus, your program should have numbers that make sense): Enter angle: 60 The Sine of 60 degrees using Taylor series is 0.2 The Sine of 60 degrees using math.sin() is 0.55 Percentage Error is: 10% The CoSine of 60 degrees using Taylor series is 0.8 The CoSine of 60 degrees using math.cos() is 0.3 Percentage Error is: 20% The Tangent of 60 degrees using Taylor series is 0.6 The Tangent of 60 degrees using math.tan() is 0.88 Percentage Error is: 5%