Yasmin Líbano
Yasmin Líbano

Reputation: 71

Recursion in C: why does my simple code doesn't work?

Given i the value of 3, the output should be the sum of 3/(3+1) + 2/(2+1) + 1/(1+1), always stopping on 1.

I can't seem to figure out what's wrong with what I did, thank you for your attention.

#include <stdio.h>
#include <math.h>

int sum_recur(int i) {
    if (i > 1) {
        return sum_recur(i - 1) / (sum_recur(i - 1) + 1);
    } else {
        return 1;
    }
}

int main(void) {
    int i;
    printf("Inform a number:\n");
    scanf("%d", &i);
    printf("%d \n", sum_recur(i));
}

Upvotes: 1

Views: 162

Answers (4)

Pawan Nirpal
Pawan Nirpal

Reputation: 622

The answers written here explain the problem reasonably well, I just want to add a little bit on your code specifically, While you do f(i-1)/f(i-1)+1 this is a terrible way of doing this and in case of a larger seed input this might cause you runtime problem with stackoverflow. So I suggest you should try something that's more efficient if at all you have to use recusion. Like this by storing the fun call value

int n = f(i-1); return n/n +1;

Upvotes: 0

VietHTran
VietHTran

Reputation: 2318

I think this would be the right recursive implementation of your formula

int sum_recur(int i){
    if(i >= 1){
        return (i / (i + 1)) + sum_recur(i-1);
    } else{
        return 0;
    }
}

Also, as @atirit mentioned, you should consider making the output to be float or double in order to preserve the floating points values of the division result. Otherwise, the result of sum_recur will always be 0 since i / (i + 1) is always 0 for integer type

#include <stdio.h>
#include <math.h>

float sum_recur(int i){
    if(i >= 1){
        return (i / (i + 1.0)) + sum_recur(i-1);
    } else{
        return 0;
    }
}

int main(void){
    int i;
    printf("Inform a number:\n");
    scanf("%d",&i);
    printf("%f \n",sum_recur(i));
}

Upvotes: 5

Uriya Harpeness
Uriya Harpeness

Reputation: 628

Here's what I believe you're trying to implement, no need for recursion, but it's there if you need it:

#include <stdio.h>

double sum_recur(double i) {
    if (i < 1) return 0;
    return i / (i + 1) + sum_recur(i - 1);
}

double sum_iter(double i) {
    double sum = 0;
    for (; i >= 1; i--) {
        sum += i / (i + 1);
    }
    return sum;
}

int main(void) {
    int i;
    printf("Inform a number:\n");
    scanf("%d", &i);
    printf("%f \n", sum_iter((double) i));
    printf("%f \n", sum_recur((double) i));
}

The summary is this:

  • There's no need for the math library.
  • Use float or double and not int - in the code itself and in the printf.
  • Correct the logic in the recursion.
  • It can be solved iteratively quite nicely.

Upvotes: 4

Mohit Singh
Mohit Singh

Reputation: 367

I don't understand what are you trying to achieve from this code. The code that you've written will always return 2.

sum_recur will always stop at 1 and you return statement will calculate 1/1+1, which is always 2. For input 3, the sum_recur will generate following:

sum_recur reaches 1 and hence returns 1 for input value 2

1/1 + 1 = 2, so for i=2 sum_recur will again return 2

This is will go on for any input number. This is why no matter what the input is, output of this recursion will always be 2. Further, what you expect this function to do in your question statement is not possible if you're using division between recursion calls. The result that you're looking to achieve is simply addition of 2 n-times or multiplication of 2 with n.

Upvotes: 0

Related Questions