2

I have this which solves a problem iteratively using a for loop. I want to convert the code to use a recursive algorithm using if-else statement that uses recursion. I've tried several times but I can't get it to work properly.

double A;
int B;
double previous=1; 
double answer;

double equation(double A,int B){
 for(int i=1; i<=B; i++){    
  answer= (A*previous)/(i+A*previous);    
  previous = answer;                  
 };
 return answer;                 
}

EDIT: Here's what I've done so far: http://pastebin.com/raw.php?i=kyeq1v5u

5
  • 1
    Is this a homework problem? If so please tag it as such. Commented Jul 12, 2012 at 14:31
  • 1
    Sorry for not tagging. Yes it is an assignment in class but I coded the for loop. Commented Jul 12, 2012 at 14:37
  • 4
    You should also show what you have tried so far. Commented Jul 12, 2012 at 14:39
  • 1
    I've added my attempt at converting it to recursion already. Commented Jul 12, 2012 at 14:47
  • 3
    Your attempt is completely missing one essential part of recursion; the recursive function call. Commented Jul 12, 2012 at 14:54

4 Answers 4

4

Have a formula. It is a recursive formula. It defines your problem, recursively

equation(A, B) =
    IF(B = 1) 
        A/(1+A)  
    ELSE
        (A*equation(B-1)) / (B+A*equation(B-1))  


Edited: There is your complete algorithm in psudo-code. all you have to do is translate to c. Good Luck.

hint: previous is equal to equation(A, B-1)

Sign up to request clarification or add additional context in comments.

3 Comments

That formula is right. But how can I code that using if-else recursive sequence?
See the hint. Think about this: how would you just have a function that just counts from 1 to B using recursion?
@imuser31250 If you need another push toward the answer, the formula I posted IS the algorithm.
2

When thinking about recursive approach, first come up with the condition that ends the recursion bearing in mind that typically recursion progresses in the opposite direction than iterative approach in this kind of functions.

In your case function signature would probably be the same, and each recursive call would have B one smaller than on previous round. Ending condition would be something that you can easily calculate, for example B=1.

Also, you don't need any global variables that you have declared in your code. Use local variables instead so they all can have different values in each recursive function call. It is also bad habit to use global variables when you can avoid them.

Comments

1

pseudo code (There might be some small mistake but it should get you thinking in the right direction

Equation A , B = Equation_internal( A, B , 1, 1)


Equation_internal (A , B , i , prev ) = 
  case i <= B   : return  Equation_internal ( A , B , i+1 , (A* prev )/(i+A*prev) )
  otherwise return prev.

8 Comments

you seem to understand recursion about as well as the op does
@Sam You mean because that's just the most general solution on how to transform a iterative into a recursive solution?
While it is technically recursive, it doesn't really apply recursive principles. The variables i and prev are superfluous.
@SamIam did i do something wrong o.0 .. this is the simplest answer i could think of
@BrianNickel post a better solution for us?
|
-2

here my little code

double equation(double A,int B);
double equation2(double A,int B, int curcount, double previous);


double A;
int B;
double previous=1; 
double answer;

int main (int argc, const char * argv[])
{


   double toto= equation(5,3);

   double toto2= equation2(5,3,0,1);


    return 0;
}

double equation(double A,int B){
    for(int i=1; i<=B; i++){    
        previous= (A*previous)/(i+A*previous);    
    };
    return previous;                 
}

double equation2(double A,int B, int curcount, double previous){

    if (curcount == B) {
        return previous;
    }else{
        curcount++;
        previous= (A*previous)/(curcount+A*previous); 

        return equation2(A,B,curcount,previous);
    }

}

12 Comments

I believe that your equation2 will result in a Stack Overflow. Also, don't post full code for homework
why stack overflow ? no, I tested and is conform with the pseudocode above. and i did not know it was a homework (and as a matter of fact pseudocode or code is the very same).
nvm, I saw the curcount++; regardless this is still an iterative function in a recursive-function's clothing
???? i am curious to see the "pure" recursive function in C. it's seem i do not have the same definition of recursive (not a problem !)
@Afungus well, I suppose that I'm happy for you that you've had your assignment's requirements fulfilled for you, but I'm sad that you did not actually learn the essence of recursion in the process. Now for the Simpler code that everyone's asking for, I had provided it from the very beginning. It's that 2 line equation in my answer. It's not in c, but the algorithm is there. I'll make 1 last edit to draw your attention to the algorithm, but I will not do your homework for you.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.