Thread: Newton's method

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    46

    Newton's method

    I have this code that should calculate the square root of a number useing newton's method.
    It compiles and runs , but while I debug ,it doesn't give the right answer for most numbers (except for the roots of 4,16,25, 36 etc)thanks for any help !

    Code:
    #include <stdio.h>
    int squareRoot(int num,int ans,int tol);
    
    int main(void)
    {
    	int num,ans,tol,root;
    	printf("Enter the number you want squarerooted");
    	scanf("%d",&num);
    	printf("Enter your guess of the number's root");
    	scanf("%d",&ans);
    	printf("Enter the pretidetermined tolerance");
    	scanf("%d",&tol);
    	root = squareRoot(num,ans,tol);
    	printf("%d%d","The square root of ",num," is:",root);
    }
    
    int squareRoot(int num,int ans,int tol)
    {
       if((ans^2 - num <= tol))
         return ans;
       else 
         return (squareRoot(num,((ans^2 + num)/(2 * ans)),tol));
    }
    Last edited by Cmuppet; 10-19-2004 at 03:08 AM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    First of all change your print statement to:
    Code:
    printf("The square root of %d is: %d\n",num,root);
    No %s needed.

    Second of all, I dont think ans^2 does what you think it does. You might want to read up on how to square a number. If you dont want to look it up, then just do ans*ans.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    46
    Thanks ! I did notice though when debugging ,and I enter say 50 for num , 7 for ans , and 0.01 for tol , it just returns 7.
    Plus when I tried typeing in a double it didn't work the way I wanted, so I changed all the occurances of int to double , and then it wouldn't compile anymore

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    In C, the ^ operator stands for the XOR(exclusive or) operator. You want to write your own square routine such as "#define SQUARE(x) ((x) * (x))" or simply a * a.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Cmuppet
    Thanks ! I did notice though when debugging ,and I enter say 50 for num , 7 for ans , and 0.01 for tol , it just returns 7.
    Plus when I tried typeing in a double it didn't work the way I wanted, so I changed all the occurances of int to double , and then it wouldn't compile anymore
    That's because you can't use bitwise operators directly on floating point numbers. (^ is a bitwise operator, as described in the post above mine)

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    46
    Thanks , it works now ! Thanks again
    Last edited by Cmuppet; 10-19-2004 at 11:05 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. on method pointers and inheritance
    By BrownB in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2009, 07:50 PM
  2. stuck on display method
    By shintaro in forum C++ Programming
    Replies: 2
    Last Post: 02-01-2009, 05:17 PM
  3. Best communication method to thousand childs?
    By Ironic in forum C Programming
    Replies: 8
    Last Post: 11-08-2008, 12:30 AM
  4. C# method
    By siten0308 in forum C# Programming
    Replies: 6
    Last Post: 07-15-2008, 07:01 AM
  5. Cubic Root by Newton's Iterative Method
    By amirahasanen1 in forum C++ Programming
    Replies: 2
    Last Post: 03-15-2005, 02:05 PM