## CS 115 Lab 8 Newton's Method (Team)

### 80 Points

Due Date: Monday, October 30, during class

Use this link to submit your team result by the Use the Lab 8 menu choice and "Code" as the type

Educational goals of this lab - verify that every student can

• import a function from the math library
• use a loop for an iterative process
• write a function definition
• call a function properly
• understand arguments and parameters
• understand using a debugger
• work together with their team to solve a larger problem

INSTRUCTIONS:

(60 points) Team Problem: Newton's method for square root

• Newton's method starts with an initial guess for the square root of a number n of n / 2. The following guess is calculated as
```        newguess = 0.5 * (oldguess + n/oldguess)
```
Note that oldguess needs to be updated repeatedly to get the next value of newguess.
• (34 points) Write a function called newton
• The function has two parameters: the number to have its square root found, and the number of iterations.
• The function should output to the screen the approximations as they are calculated.
• The function should return the final value of the process.
• The function should have a standard header with the 3 P's.
• (26 points) Write a main function to be the driver for the newton function, It will ask for a number to find the square root of and the number of iterations desired. The first number should be a float, the second an integer. The driver should show the difference between the result of newton and the math library sqrt function result.
• Note the driver handles a negative number to find the square root of by taking the absolute value of the input.
• The main function should have a header comment as well.
• Sample run
```Enter the number you want the square root of: 25
The number of iterations? 10

Iteration 1 approximation 12.5
Iteration 2 approximation 7.25
Iteration 3 approximation 5.349137931034482
Iteration 4 approximation 5.011394106532552
Iteration 5 approximation 5.000012953048684
Iteration 6 approximation 5.000000000016778
Iteration 7 approximation 5.0
Iteration 8 approximation 5.0
Iteration 9 approximation 5.0
Iteration 10 approximation 5.0

sqrt of 25.0 from math library function 5.0
Difference 0.0
```

Another run

```Enter the number you want the square root of: 27
The number of iterations? 5

Iteration 1 approximation 13.5
Iteration 2 approximation 7.75
Iteration 3 approximation 5.616935483870968
Iteration 4 approximation 5.211913542366209
Iteration 5 approximation 5.196176253962744

sqrt of 27.0 from math library function 5.196152422706632
Difference 2.383125611160608e-05

```

Run with a negative number input

```Enter the number you want the square root of: -8
The number of iterations? 10

Iteration 1 approximation 4.0
Iteration 2 approximation 3.0
Iteration 3 approximation 2.833333333333333
Iteration 4 approximation 2.8284313725490193
Iteration 5 approximation 2.8284271247493797
Iteration 6 approximation 2.82842712474619
Iteration 7 approximation 2.82842712474619
Iteration 8 approximation 2.82842712474619
Iteration 9 approximation 2.82842712474619
Iteration 10 approximation 2.82842712474619

sqrt of 8.0 from math library function 2.8284271247461903
Difference -4.440892098500626e-16
```
• (10 points) Test cases
• Write 6 test cases for this program. Include them in a comment at the bottom of the program.
• Three of them should test the number of iterations in loop in the newton function.
• Three of them should test the number to take the square root of.
• Give the usual components of a test case (description, inputs, expected output) on one line with commas between.
• Submit your finished program with the link at the top of the page. Use the Lab 8 and Code menu choices.
• (10 points) Use the debugger (either IDLE's or WingIDE's)
• Use a debugger to set a breakpoint at the newton function call in the main.
• Show your TA that you can step into the newton function definition.
• Step through one iteration of your newton function's loop.

Log off properly - you don't want your account misused by someone else!

Remember NOT to leave files on the local hard drives in this lab or anywhere else on campus! Make sure you save your projects onto a portable storage device you take with you!