## Goal

• Learn how to compute square roots efficiently.
• Learn to use functions, while loops and for loops.

## Checklist

• Use the following submit command: /u/cs126/bin/submit 1 readme bisect.c bisect2.c newton2.c. Do not use different file names. (Put each assignment in a different directory; that way you won't overwrite your old readme file. Ask a lab TA if unsure.) Do not submit a.out. Check the course roster (on the General Information page) to make sure that we have your name in our database and that you are attending the right precept. If not, contact the Course Manager.

• In precept we will cover the basics of the bisection method and Newton's method.

• name, precept number
• description of problems encountered and high level description of code
• output of bisect.c with p(x) = 6 - x - x^3 and epsilon = 0.000001.
• output of bisect2.c (with epsilon = 0.000001) which computes the square roots of 2 to 20 in a table, along with number of calls to function f required by bisection method for each square root computation
• output of newton2.c (with epsilon = 0.000001) which computes the square root of 2 to 20 in a table, along with number of calls to function f (or its derivative) required by Newton's method for each square root computation
• bisect.c, bisect2.c
• common mistakes lead to infinite loop, debug using printf method of printing out the values of all relevant variables to pinpoint error
• bad programming practice to hardwire any arbitrary numbers (like 0.000001) into code - instead assign variable epsilon = 0.000001, then if you want to change precision you only need to change one line of code
• can use math library function pow(x,y) to compute x^y (need to #include <math.h> before use) for 7th root of 126 - watch out for infinite loop due to floating point precision!
• newton2.c
• OK to choose 1.0 as initial guess
• Terminate when interval is small, not when function value is small.
• Do not use the math library function fabs. You can write your own, if you like.

• Check out the course Frequently Asked Questions list. Many common questions are addressed here.

• Customize emacs to automatically indent your code and list line numbers. Also, you can now compile your code without leaving emacs by typing Ctrl-c Ctrl-c. If there is a syntax error, typing Ctrl-c Ctrl-n will automatically highlight it for you. Check the FAQ page or ask a lab TA for assistance.

• Change your shell to enable filename completion and command recall. Check the FAQ page or ask a lab TA for assistance.

• Change the default version of Netscape from 3.5 to 4.05. Check the FAQ page or ask a lab TA for assistance.

• ## Four Star Challenge (for hackers only)

Any student with a correct solution will be exempt from the first midterm!
• The above methods find a real root of a given function of a single variable. Write a C program that determines whether or not a given polynomial function has an integral root. For example, if f(x) = 4x^3 - 8x^2 + 5x - 1, then x = 1 is the unique integral root. The challenge is to extend your program to handle arbitrary multivariate polynomials as well. For example, if f(x,y,z) = 6x^3yz^2 + 3xy^2 - x^3 - 10 then (x,y,z) = (5,3,0) is an integral root.

• Kevin Wayne