cant get my function to work!

This is a discussion on cant get my function to work! within the C Programming forums, part of the General Programming Boards category; in the following program i am trying to compte the square root usiing a function. everything works except i keep ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    11

    cant get my function to work!

    in the following program i am trying to compte the square root usiing a function. everything works except i keep getting the number one printed out as the square root of any number. here is the code, thanks for your help!
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    main() {
    
      double square_root (double, double);
      double tolerance;
      double input;
      double ans;
    
      printf("Input tolerance for Newton's method: ");
      scanf("%le", &tolerance);
      printf("Input a non-negative number: ");
      scanf("%le", &input);
      ans = square_root (input, tolerance);
    
      printf("The square root of: %1.15lf is %1.15lf\n", input, ans);
    
    }
    
    //function
    
    double square_root (double i, double t) {
      double r = 1;
      while( fabs((r*r)-i) <= t ) {
        r = (i/(r + r)) / 2;
      }
      return r;
    }
    Code tags added by Kermi3

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    Code Tags

    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happier about helping you

    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found on the code tag post at the top of every forum. I also suggest you take a look at the board guildlines if you have not done so already.

    This is a common first post mistake, just remember to use [code] tags in the future and you'll get much more help.

    If this is your first time posting here the welcome, and if there's anything I can do or any questions I can answer about these forums, or anything else, please feel free and welcome to PM me.


    Good Luck with your program,

    Kermi3
    Lead Moderator
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Add a line for debugging.
    Code:
    double square_root (double i, double t) {
      double r = 1;
      while( fabs((r*r)-i) <= t ) {
        r = (i/(r + r)) / 2;
        printf("r = %f\n", r);
      }
      return r;
    }
    Was the loop ever entered?

    Maybe you want something more like this.
    Code:
    #include <stdio.h>
    #include <math.h>
    
    /* http://www.cs.mtu.edu/~shene/COURSES...06/sqrt-1.html */
    double square_root(double i, double t)
    {
       double r = i, x = i;
       for ( ;; )
       {
          r = ( (i / r) + r ) / 2.0;
          if ( fabs(x - r) <= t )
          {
             break;
          }
          x = r;
       }
       return r;
    }
    
    int main()
    {
       double ans, input, tolerance = 0.000001;
       for ( input = 1.0; input < 11.0; input += 1.0 )
       {
          ans = square_root (input, tolerance);
          printf("square_root(%f) = %f\n", input, ans);
       }
       return 0;
    }
    
    /* my output
    square_root(1.000000) = 1.000000
    square_root(2.000000) = 1.414214
    square_root(3.000000) = 1.732051
    square_root(4.000000) = 2.000000
    square_root(5.000000) = 2.236068
    square_root(6.000000) = 2.449490
    square_root(7.000000) = 2.645751
    square_root(8.000000) = 2.828427
    square_root(9.000000) = 3.000000
    square_root(10.000000) = 3.162278
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    11

    thanks for your help

    thanks for your help, i got it working now. And Ill remember to use the [code] tag, its easy to do and saves everyone a long time!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM

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