Download 1 Math Library 2 Simultaneous Assignment

yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
MIS 126: Programming Assignment 2
Algorithm for Computing Square Roots
Due by 10:30am on Wednesday, September 16th
Our second programming assignment will look at the math library.
Math Library
We saw in class that you can get a Python program to execute mathematical
commands by importing and using the math library. Some of the math
library functions include
Python Mathematics
An approximation of pi
The square root of x
The sine of x
The cosine of x
Recall that in order to use the functions provided by the math library, we
need to import the math library into our programs using the statement
import math
Then, to use one of the math library functions we need to use the dot notation
to tell Python that the function is located in the math library/module. For
example, we write
to compute x.
Simultaneous Assignment
Simultaneous assignment allows us to calculate several values all at the same
time. Recall that simultaneous assignment can be used to get multiple numbers from the user in a single input. For example, if we wanted to get two
numbers from the user in a single input, we could write
num1, num2 = eval(input("Enter two numbers separated by a comma: "))
You will be using simultaneous assignment in your code for this project.
A Programming Project
You have seen that the math library contains a function that computes the
square root of numbers. In this program, you are to write your own algorithm
for computing square roots (not using the built-in Python function). One
way to solve this problem is to use a guess-and-check approach. You first
guess what the square root might be and then see how close your guess is.
You can use this information to make another guess and continue guessing
until you have found the square root (or a close approximation to it). One
particularly good way of making guesses is to use Newton’s method. Suppose
x is the number we want the root of and guess is the current guessed answer.
The guess can be improved by using
guess +
as the next guess.
Write a program that implements Newton’s method. The program should
prompt the user for x (the value to find the square root of) and n (the
number of times to improve the guess). This input should be done using
a simultaneous assignment. Starting with a guess value of guess = x/2,
your program should loop the specified number of times applying Newton’s
method and report the final value of guess. You should also subtract your
estimate from the value of math.sqrt(x) to show how close it is.
A good way to start any program is to map out a skeletal version of it,
showing the major control structures (the loops and conditionals) and not
much else. Here’s a skeleton for our program:
#import the math library here
def main():
#Prompt user to input the value for x and n, use simultaneous assignment
#Compute the first guess
#Loop the specified number of times - should this be a for or while loop?
#Update the guess
#When the loop ends, our program comes here
#Print out the value of guess
#Compute the square root using math.sqrt(x)
#Print out the difference between guess and math.sqrt(x)
Assignment: Create a new Python file. Type this skeleton in, and
then insert code to fulfill all the steps. When the program works, you are
done. You need to submit the Python .py file on Blackboard by 10:30am
on Wednesday, September 16th.
Here are some of the things I will be looking for when I grade your assignment
• Did you use comments to describe what your code is doing? You need
to add more comments than what is given in the program skeleton.
This is a good programming practice and also helps me follow your
logic (especially if there are any bugs/errors in the code).
• Did you use variable names that are descriptive? This is a good programming practice I would like you to follow.
• Does your program work? I will run and test your program with various
user input for x and n.
• Does your program use anything that we haven’t covered in class? You
can create this program with everything we did in class/everything
contained in this packet. Please do not copy any code from the Internet
and pass it off as your own.
I am happy to help, so if you have any questions, please ask! If you email me
asking for help, please send a copy of your code so I can give you meaningful