Thread: Help with square root using pseudocode

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    28

    Help with square root using pseudocode

    Code:
    #include "stdio.h"float squareRoot(float number, float accuracy);
    int main()
    {	
       
    	float a;
            printf("\nsquare root test 1: enter a number\n");
            scanf("%f",&a);
            printf("root(%.2f) = %.4f\n", a, squareRoot(a, 0.00001));
            
            getchar();
            return 0;
    }
    
    
    float squareRoot(float num, float accuracy)
    {	
    	float guess = 1;
    	float difference = num - guess * guess;
    	float x;
    	while(difference > accuracy)
    	{
    		x = num/guess;
    		guess = (guess + x)/2;
    		difference = num - guess * guess;	
    	}
    	return x;
    }
    I am currently trying to figure out what went wrong, my best guess is my while loop is wrong because it returns the answer x through only 1 iteration(1 loop) that is it goes through the while loop without even checking if difference>accuracy(0.0001) can anyone help out? thank you.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Things like this use a absolute function on the difference look for abs or fabs in maybe math header/lib.

    fabs - C++ Reference

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    Yes I have that #include "stdlib.h" I didn't copied it

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by AFCKING View Post
    Yes I have that #include "stdlib.h" I didn't copied it
    If this was supposed to make sense; it does NOT make any sense to me.

    Do you realize that -2 is less than 1?
    And, abs(-2) is NOT less than 1?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    Right! since it returns a negative value for difference I need to use the absolute value. Thanks. Haha it took me long enough to understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Square Root
    By chillycoke in forum C Programming
    Replies: 26
    Last Post: 06-27-2011, 07:43 AM
  2. square root
    By Noobie in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2003, 11:01 AM
  3. Square root
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 07-05-2002, 06:35 AM
  4. how do u get the square root
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 06-11-2002, 04:36 PM
  5. square root
    By help in forum C Programming
    Replies: 5
    Last Post: 08-29-2001, 05:46 AM