Solved

recursive problem

Posted on 2008-10-05
11
296 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:mikeregas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 22646982
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
 

Author Comment

by:mikeregas
ID: 22647108
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22647118
>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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:lakshmikarle
ID: 22647340
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
 
LVL 1

Expert Comment

by:lakshmikarle
ID: 22648784
sunnycoder,
I will take care in future
0
 

Author Comment

by:mikeregas
ID: 22650155
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22650289
http:#22647118 lists your problem
http:#22646982 hints at a solution
0
 
LVL 1

Expert Comment

by:lakshmikarle
ID: 22650877
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
 

Author Comment

by:mikeregas
ID: 22651030
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
 
LVL 1

Accepted Solution

by:
lakshmikarle earned 500 total points
ID: 22651108
n can take values like 0,1,2,3
Assume x(0) = 1.
Go thru the link I have posted
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question