Thread: program to calculate the square root

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    15

    Cool program to calculate the square root

    Hello,

    I am writing a program that will repeatedly calculate (loop) using the formula

    nextGuess = 0.5(lastGuess + number/lastGuess).

    I need a function using this calculation, and I need some inputs: the number, the first guess from the user.

    Then, the difference will be calculated between the value of nextGuess and the value of lastGuess. If the difference is < 0.005, the loop ends.

    Here is the code I have written, so far. It compiles, but definitely doesn't do what I want.
    Code:
    #include <stdio.h>
    #include <math.h>
    
    void instruct( void );
    double nextGuess (double number, double guess, double lastGuess  );
    
    
    int main( void )
    
    {
    
        double    number,
            answer,
            guess,
            difference,
           	newGuess,
            lastGuess;
    
    
        instruct ();
    
        printf ( "\n\n Enter a real number that you wish to have the square root calculated: \n\n" );
        scanf ( "%lf", &number );
    
        if ( number < 0 )
           printf ( "\n\n I'm sorry but negative numbers don't work in this equation. \n\n" );
    
        else if    ( answer = (sqrt(number)))
    
    	printf ( "\n\n Enter your guess for the square root of %.5lf: \n", number );
            scanf ( "%lf", &guess );
    
        do
    
        {
    
            
    
    //        newGuess = 0.5 * ( lastGuess + newGuess / lastGuess )
    
    		
    
    	guess = nextGuess(number, guess, lastGuess);
    
        difference = answer - guess;
    
    }
    
        while( difference > 0.005 || difference < -0.005 );
    
        printf ( "\n\n Congratulations, your guess was within 0.005 of the square root of %.5lf!!!!! \n\n", number );
    
        return 0;
    
    }
    
    void instruct( void )
    
    {
    
        printf ( "\n\n You will give a number, N, which will then have its square root calcuated. You will then insert guesses as to what you think the square root is and when are you within 0.005 of the correct answer you will have completed the situation. \n" );
    
    }
    
    double nextGuess (double number, double guess, double lastGuess  )
    {
    	double newGuess;
    
    	 newGuess = 0.5 * (lastGuess + newGuess/ lastGuess);
    
    return (newGuess);
    }
    Thanks for any help! I'm fairly new to programming, so I am kind of lost as to how to fix it...

    Sarah

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Compare and contrast:
    nextGuess = 0.5(lastGuess + number/lastGuess)
    Code:
     newGuess = 0.5 * (lastGuess + newGuess/ lastGuess);

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by saszew View Post
    Hello,

    I am writing a program that will repeatedly calculate (loop) using the formula

    nextGuess = 0.5(lastGuess + number/lastGuess).

    I need a function using this calculation, and I need some inputs: the number, the first guess from the user.

    Then, the difference will be calculated between the value of nextGuess and the value of lastGuess. If the difference is < 0.005, the loop ends.

    Here is the code I have written, so far. It compiles, but definitely doesn't do what I want.
    Code:
    #include <stdio.h>
    #include <math.h>
     
    void instruct( void );
    double nextGuess (double number, double guess, double lastGuess  );
     
     
    int main( void )
     
    {
     
        double    number,
            answer,
            guess,
            difference,
               newGuess,
            lastGuess;
     
     
        instruct ();
     
        printf ( "\n\n Enter a real number that you wish to have the square root calculated: \n\n" );
        scanf ( "%lf", &number );
     
        if ( number < 0 )
           printf ( "\n\n I'm sorry but negative numbers don't work in this equation. \n\n" );
     
        else if    ( answer = (sqrt(number)))
     
        printf ( "\n\n Enter your guess for the square root of %.5lf: \n", number );
            scanf ( "%lf", &guess );
     
        do
     
        {
     
     
     
    //        newGuess = 0.5 * ( lastGuess + newGuess / lastGuess )
     
     
     
        guess = nextGuess(number, guess, lastGuess);
     
        difference = answer - guess;
     
    }
     
        while( difference > 0.005 || difference < -0.005 );
     
        printf ( "\n\n Congratulations, your guess was within 0.005 of the square root of %.5lf!!!!! \n\n", number );
     
        return 0;
     
    }
     
    void instruct( void )
     
    {
     
        printf ( "\n\n You will give a number, N, which will then have its square root calcuated. You will then insert guesses as to what you think the square root is and when are you within 0.005 of the correct answer you will have completed the situation. \n" );
     
    }
     
    double nextGuess (double number, double guess, double lastGuess  )
    {
        double newGuess;
     
         newGuess = 0.5 * (lastGuess + newGuess/ lastGuess);
     
    return (newGuess);
    }
    Thanks for any help! I'm fairly new to programming, so I am kind of lost as to how to fix it...

    Sarah
    That doesn't look right, Sarah.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Why not be a little more simplistic?

    Example:
    Code:
    #define TOLERANCE  0.00005
    
    static double sqrt_internal(double guess, double last)
    {
      return 0.5 * (last + guess/ last);
    }
    
    double mysqrt(double number)
    {
      double root;
    
      do
      {
        root = sqrt_internal(number, 1.0);
      } while((root*root - number) <= TOLERANCE);
    
      return root;
    }

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    15

    Thanks

    Thanks for your suggestions! I think I have been staring at the screen for too long. I might just scrap what I have and start fresh tomorrow.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    The ability to scrap an idea and start anew is important in learning to program. Keep that attitude and you will go far in the field.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    15
    yeah, i had to get the wrong answer out of my head to figure out some steps to the right answer!

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I love when you scrap something after toiling on it for hours on end and the second attempt ends up being like 15 lines as opposed to 412 lines.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  2. Characters in a Magic Square Program
    By TeamGreen in forum C++ Programming
    Replies: 1
    Last Post: 03-16-2004, 08:08 AM
  3. Finding the square root! Not Working!
    By Lah in forum C Programming
    Replies: 5
    Last Post: 09-14-2003, 07:28 PM
  4. Templated Binary Tree... dear god...
    By Nakeerb in forum C++ Programming
    Replies: 15
    Last Post: 01-17-2003, 02:24 AM
  5. Square Root
    By Kyoto Oshiro in forum C++ Programming
    Replies: 5
    Last Post: 09-05-2002, 01:22 AM