Note that there are some explanatory texts on larger screens.

plurals
  1. POmath.sqrt vs. Newton-Raphson Method for finding roots in c#
    primarykey
    data
    text
    <p>I'm doing a homework project that requires this:</p> <p>Below you will find the code that I have written to compute the square root of a number using the Newton-Raphson method. Include it in your project. For this project your job will be to write a test harness that tests the code that I have written. Carefully read the method prologue to understand how the function should work. Your test harness will provide a loop that:</p> <ol> <li>Prompts the user to enter in a test value.</li> <li>Gets the user's input. If a zero is entered, your program will print out a report and terminate.</li> <li>Calls the Sqrt method provided in this project, and saves the return value in a double variable.</li> <li>Calls the Sqrt method that is built into the Math class and saves the return value in a second double variable.</li> <li>Compare these two values to see if they are equal.</li> <li>When the user indicates that they are done (by entering a zero) display a report that shows this information: * How many test cases you executed * How many passed * How many failed</li> </ol> <p>So I've done all this without any problems in about 15 minutes, however for extra credit he asks us to find what is wrong with his Sqrt method and fix it so its return value equals the Math.Sqrt return value of the .net framework. I can't seem to find the problem in his method, and I want to find it, so I was wondering if anyone could point me in the right direction as to what the problem is with his Sqrt method? Thanks.</p> <p>Here is my complete code:</p> <pre><code>// declare variables double userInput = 0.0; double debrySqrtReturnValue = 0.0; double dotNetSqrtReturnValue = 0.0; int testCasesExecuted = 0; int testsPassed = 0; int testsFailed = 0; bool isEqual = false; do { // Prompt the user to enter in a test value Console.Write("Please enter a positive integer value: "); userInput = double.Parse(Console.ReadLine()); if (userInput != 0) { debrySqrtReturnValue = Sqrt(userInput); dotNetSqrtReturnValue = Math.Sqrt(userInput); Console.WriteLine("The square root of {0} is: {1}", userInput, debrySqrtReturnValue); Console.WriteLine("The real square root of {0} is: {1}\n", userInput, dotNetSqrtReturnValue); if (debrySqrtReturnValue == dotNetSqrtReturnValue) isEqual = true; else isEqual = false; if (isEqual) testsPassed++; else testsFailed++; testCasesExecuted++; } } while (userInput != 0); Console.WriteLine("\n\n--------------------------------Report---------------------------------"); Console.WriteLine("test cases excecuted: {0}", testCasesExecuted); Console.WriteLine("tests passed: {0}", testsPassed); Console.WriteLine("tests failed: {0}", testsFailed); Console.ReadLine(); } // The Sqrt method // Purpose: to compute the square root of a number // Parameters: a positive, non-zero integer // returns: a double, which is the square root of the number // --------------------------------------------------------- static double Sqrt(double number) { // constants to use in the calculation const int FIRST_APPROX = 2; const double EPS = 0.001; // a local variable double xN = 0; // pick 2 as first approximation double xNPlus1 = FIRST_APPROX; do { xN = xNPlus1; xNPlus1 = xN - ((xN * xN - number) / (FIRST_APPROX * xN)); } while (Math.Abs(xNPlus1 - xN) &gt; EPS); return xN; } </code></pre> <p>} </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload