Thread: Need Some Assistance

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    6

    Need Some Assistance

    I have been trying to figure this out for a long time now. My program compiles but i keep getting the incorrect answer. Here are the instructions:



    Write a function called SquareRoot that takes in a single parameter (type double) and returns an approximated square root (also a double), using the following calculation process:

    The square root of a number, num can be approximated by repeatedly performing a calculation using the following formula:

    nextGuess = (lastGuess + (num / lastGuess)) / 2

    When nextGuess and lastGuess are almost identical, nextGuess is the approximated square root. You can start the initial guess (i.e. the first value of lastGuess at 1.0, for the first computation. Once the difference between nextGuess and lastGuess is less than a small number (use the value 0.0001), you may claim that nextGuess is the approximated square root of num. Format your square root result to 5 fixed places after the decimal point. Original value entered by user should be printed in the default format. (You may NOT use the square root function in the math library -- the point of this exercise is to write your own function).

    To test this function, write a main() routine that prompts the user and lets them enter a number (of type double), then passes the number into your square root function and prints out the returned value.

    Sample run 1: (user input underlined)

    Input Number: 123
    square root of 123 = 11.09054



    Here is my code :

    Code:
    #include <iostream>
    using namespace std;
    
    double squareroot (double num1);
    
    int main()
    {
     double num1;
     cout << "Enter a value " ;
     cin >> num1;
     double sqt = squareroot (num1);
        
     cout << "The square root of : " << sqt << endl;
    
    return 0;
    }
    
    double squareroot(double num1)
    {
    
     double lastGuess = 1.0;
     double nextGuess;
        
     nextGuess = (lastGuess + (num1 / lastGuess))/2.0;
    
    while(!( (nextGuess <= (lastGuess + 0.0001)) && (nextGuess >= (lastGuess - 0.00001))) )
    {
     nextGuess = (lastGuess + (num1 / lastGuess))/2.0;
     num1 =nextGuess;
    }
    
    return nextGuess;
    }
    






    The output keeps coming out as 1.000008.....i assumed that the values are not getting passed from the main function. Please assist. Thank You.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > num1 =nextGuess;
    How about
    lastGuess = nextGuess;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    6
    lastGuess = nextGuess;



    Hey thanks for the help, but that change still didn't work, my answer now went up to 62....any other suggestions will be greatly appreciated....

    Thanks.....

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    line 1: nextGuess = (lastGuess + (num1 / lastGuess))/2.0;
    line 2: lastGuess = nextGuess;

    Try placing line 2 before line 1.
    You're only born perfect.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well the method is well documented, all it takes is a little "nouse" to translate that into code.
    The hard part was the exit condition, beats me how you managed to goof on the assignment.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    6
    Well the method is well documented, all it takes is a little "nouse" to translate that into code.The hard part was the exit condition, beats me how you managed to goof on the assignment.


    uuuhhh...maybe cause it's the first program that i have ever written...

    but thanks anyway...

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    6
    Try placing line 2 before line 1.

    That worked...thanks alot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hello,i need assistance in completing the code
    By toader in forum C++ Programming
    Replies: 1
    Last Post: 06-22-2009, 03:32 AM
  2. Variable Timer assistance
    By rich2852 in forum C Programming
    Replies: 2
    Last Post: 01-21-2009, 05:43 AM
  3. Any assistance would be greatly appreciated
    By iiwhitexb0iii in forum C Programming
    Replies: 18
    Last Post: 02-26-2006, 12:06 PM
  4. Need some more assistance
    By Thantos in forum Windows Programming
    Replies: 6
    Last Post: 08-14-2003, 12:13 PM
  5. Need assistance for major text base Arena RPG project
    By Ruflano in forum C++ Programming
    Replies: 0
    Last Post: 04-04-2002, 11:11 AM