We help IT Professionals succeed at work.

recursive problem

on
331 Views
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);
}
``````
Comment
Watch Question

View Solution Only

CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
Are you sure you have the correct recursion?
The method you use looks like it would work better passing 3 numbers.
or else changing

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.
CERTIFIED EXPERT
Top Expert 2006

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

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

Commented:
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
``````

Commented:
sunnycoder,
I will take care in future

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.
CERTIFIED EXPERT
Top Expert 2006

Commented:
http:#22646982 hints at a solution

Commented:
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.

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
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

Deciding to stick with EE.

Mohamed Asif

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Connect with Certified Experts to gain insight and support on specific technology challenges including:

• Troubleshooting
• Research
• Professional Opinions
Unlock the solution to this question.

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.