Assignment 3, due Thursday February 10 at noon.

You must submit your program electronically via gsubmit on csa.

 Under no circumstances will late assignments be accepted.

 The code you submit must conform with the programming guidelines.


Program Description

Submit one file called root.cpp.  In this file write one function

double root(double a, int n, double x0, double epsilon);

that finds the n-th root of a positive floating point number a via Newton-Raphson approximation.

Your function should detect the following error cases: In all error cases, your function should return the value -1.

Your function should have no side-effects (e.g., no printing error messages).

Newton-Raphson Approximation

The best known iterative method for computing roots is Newton-Raphson approximation.  To find the zero of a function whose derivative is also known, compute

This method actually yields the same algorithm for finding square roots as does the classical Greek method.  Finding sqrt(a) is the sames as finding the zero of f(x) = x^2 - a.  Thus,

Given an initial value for x (x0), the above equation is evaluated iteratively until the change in the value of x is less than a numerical tolerance, epsilon:

This method generalizes to find n-th roots.  In this assignment we find the n-th root via Newton-Raphson, by finding the zero of the function f(x) = x^n - a.

Hint: You are welcome to use the math function pow().

Notes: Only submit the function double root(double a, int n, double x0, double epsilon) in root.cpp.  Do not submit a program main(). You cannot alter the function name nor arguments as specified in the function prototype given for root.  Your function should not have any side effects.

Testing: You are responsible for thoroughly testing your program to make sure that it works. In grading your program, we will test your program on some standard tests. Be sure that your function can correctly find the root when n>0, and when n<0. 

Grading Criteria

Refer to the grading criteria file: p3.criteria


Academic Honesty and Collaboration

Cooperation is recommended in understanding various concepts and system features. But the actual solution of the assignments, the programming and debugging must be your individual work, except for what you specifically credit to other sources. (Your grade will be based on your own contribution.) For example, copying without attribution any part of someone else's program is plagiarism, even if you modify it and even if the source is a textbook. The University takes acts of cheating and plagiarism very seriously: first time violators are routinely suspended for a semester.