• C

recursive problem

I have a recursive program that is suppose to return the square root of a number recursivley, the program will run, but it returns 1. please help
#include <stdio.h>
 
 
 
double square_root(double low, double high);
 
int main(void)
{
    float number;
    double root;
    
    printf("Enter a number to compute the square root of.\n");
    scanf("%f", &number);
    
    root = square_root(1, number);
    
    printf("The square root of %.3f is about %.3lf\n", number, root);
    
    system("PAUSE");
    return 0;
}
 
	double square_root(double low, double high)
{
    float mid;
    
    mid = (high + low) / 2;
 
    //Base Case
    if((mid*mid) == high)
        return mid;
 
     //Stops when the high and low numbers are within 0.001 of one another.   
    if((high - low) < 0.001)
        return mid;
    
    if((mid*mid) > high)
        return square_root(low, mid);
        
    return square_root(mid, high);
}

Open in new window

mikeregasAsked:
Who is Participating?
 
lakshmikarleConnect With a Mentor Commented:
n can take values like 0,1,2,3
Assume x(0) = 1.
Go thru the link I have posted
0
 
ozoCommented:
Are you sure you have the correct recursion?
The method you use looks like it would work better passing 3 numbers.
or else changing
0
 
mikeregasAuthor Commented:
I need to take a number say 19.7 and then it is suppose to return 4.438 is the example that I was given. I know that I am suppose to take two number the low which would be 0 and the high and which would be 19.7 and recursively bring it down to an approximate square root. So there would be a third number in the equation the  mid.
0
Worried about phishing attacks?

90% of attacks start with a phish. It’s critical that IT admins and MSSPs have the right security in place to protect their end users from these phishing attacks. Check out our latest feature brief for tips and tricks to keep your employees off a hackers line!

 
sunnycoderCommented:
>square_root(1, number);
second arg is the number whose sq root you want.

Your recursive calls are
>return square_root(low, mid);<<<------ you are calculating sq root of mid!!
>return square_root(mid, high);

0
 
lakshmikarleCommented:
Hello mikergas,

You are using "Babylonian method" to calculate square root. You are not using it in right way.
For proer desciption of babylonian method can be found in link
http://personal.bgsu.edu/~carother/babylon/Examples.html

I am attaching modified and working version of your code..
<code edited out>
sunnycoder
ZAPE

Open in new window

0
 
lakshmikarleCommented:
sunnycoder,
I will take care in future
0
 
mikeregasAuthor Commented:
I am confused on what is right in my code and what is wrong? can you please give me some direction to go in. my code is return a 1 for any number that I input and I know that is worng.
0
 
sunnycoderCommented:
http:#22647118 lists your problem
http:#22646982 hints at a solution
0
 
lakshmikarleCommented:
According to algorithm
x(n+1) = 1/2( x(n) + a / x(n) )

where a is the number for which square root should be calculated.
Change calculation of 'mid' to above formula.

Also, since you are always calling function recursively with low as 1, your answer is always converging to 1.

0
 
mikeregasAuthor Commented:
x(n+1) = 1/2( x(n) + a / x(n) ) let me make sure that I understand this. if a is the number that I am trying to get the root of then x is a function of what? I am truely lost with recursion
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.