# cant get my function to work!

• 05-08-2003
scaven
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; }```
• 05-08-2003
kermi3
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.

Kermi3
• 05-08-2003
Dave_Sinkula
```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; }```
```#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 */```