Program not working

This is a discussion on Program not working within the C Programming forums, part of the General Programming Boards category; Hello I wrote this program to find the square root of any number that the user enters using newtons method ...

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

    Program not working

    Hello I wrote this program to find the square root of any number that the user enters using newtons method but it is not working. The initial guess is the number entered by the user divided by 10. And after running through equation and calculating the new value it compares it with the old value(xm1) and if the difference between them is less then 0.001 then it prints out the value it calculated. I would be very greatful if someone can corrent my mistake or point out to me where I made the mistake.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    #define SIZE 5
    
    int main()
    {
    	float x, number,xm1,r;
    	float difference;
    	int a;
    
    	printf("Please enter the number of which you wish the square root of: ");
    	scanf("%f",&number);
    
    	a=number/2;	
    	xm1=0;
    	difference = 1;
    
    	x = a - (pow(a,2)-number)/(2*a);
    	xm1=x;
    
    	while(difference >= 0.01)
    	{
    		r=  xm1- (pow(xm1,2)-number)/(2*xm1);
    
    		difference = r-xm1;
    
    		xm1=r;
    
    	}
    
    	if(difference <= 0.01)
    		{
    			printf("the square root is %f\n",r);
    		}
    
    	return 0;
    }

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    One problem you are going to run into is precision. Comparing floating point numbers is fraught with difficulties. There are a number of links in this post that have material devoted to floating point numbers.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I'm assuming the problem you're running into (you forgot to mention what it was) is that you failed to compare fabs(difference) to 0.001, rather than just difference?

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    92
    How am I meant to do that I thought that it keeps in the while loop until the difference is <= 0.01. So where am I exactly going wrong becasue I don't understand what you are saying.

  5. #5
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    489
    What is your actual problem?
    The newton's method of finding square roots yields approximate results and should be expected.
    Edit:
    It looks like your initial guess plays a major role in how approximate the result would be.
    The initial guess is the number entered by the user divided by 10
    I think you meant 2.
    Last edited by stevesmithx; 01-11-2009 at 01:26 AM.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by HAssan View Post
    How am I meant to do that I thought that it keeps in the while loop until the difference is <= 0.01. So where am I exactly going wrong becasue I don't understand what you are saying.
    So what if the difference is -12345578? That's less than 0.01, but you sure as heck don't want to stop. You need to check whether the absolute value of the difference is less than 0.01.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Replies: 5
    Last Post: 02-02-2003, 09:56 AM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  4. Simple Program not working right (in C)
    By DruzeTito in forum C Programming
    Replies: 5
    Last Post: 06-01-2002, 10:14 PM
  5. Program ive been working on called ChatMate
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-23-2002, 08:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21