tags:

views:

195

answers:

6

My funciton takes a number input from the user and recursively sums the number 'n' to one.
Inputting a 5 would sum 1/5 + 1/4 + 1/3+ 1/2 + 1/1.

#include<stdio.h>
#include<conio.h>
//to 
float recursion(float num,float sum);
void main(void)
{
  float num=5,sum=0;
  //input num
  printf("%d",recursion(num,sum));

  getch();
}

float recursion(float num,float sum)
{
  // int sum=0; every time u run it the sum is assigned 0
  if( num==1)
    return 1;
  else
  {
    sum=sum+(1/num);
    num--;
    recursion(num,sum);
  }
  return sum;
}//recursion function ends

The problem is, that it is giving 0 :/ Can anyone help, please?

A: 

Hi! Use sum=sum+(1.0/num);. When you divide 1, an integer with a float, the float gets converted to integer first.

WebMonster
num is a float, 1/num gets converted to double already.
Alexandre C.
I believe the divide operator, when giving a floating point operand and an integral operand, will automatically return a floating-point result.
... err what Alexandre said.
See http://en.wikibooks.org/wiki/C++_Programming/Type_Casting (section promotion / denotion)
Alexandre C.
+5  A: 

You should return the result of the recursive call:

return recursion(num,sum);

instead of return sum.

tanascius
And `%d` should be changed to `%f`.
tur1ng
+1. I think this is at least one of the bigger problems the OP is having. There's nothing wrong with the division.
@tur1ng: good point, that explains the 0 instead of 0.2
tanascius
@tanacius but i think that a function just returns only 1 value
fahad
but the answer shdnt be 0.2 it should be giving 2.28
fahad
@fahad ... yes, it does. You can just delete the line `return sum;` because it will never be reached. Right now you calculate only 1/5 - which is 0.2 and showed is 0, because you use `%d` instead of `%f`
tanascius
but i want to calulate the 2.28 not 0.2 :S
fahad
@fahad: If you change your function to return the result of the recursion like tanascius said, you will get 2.28. And if you also change the print statement like tur1ng said, you will also see it. Simple as that.
sepp2k
A: 
float recursion(float num) {
  if( num==1) 
    return 1; 
  return (1.0/num) + recursion(num - 1);
}

By the way, do not input a negative number!

OMG_peanuts
A: 
float recursion(int num) {
  if (num == 0) {
    return 0;
  }

  return 1 / num + recursion(num--);
}
Codler
Pretty sure this will always return 0 due to integer division.
IVlad
This is actually undefined behavior (using `num` and `num--` without a sequence point between them). And even if it wasn't, it wouldn't do what you want because num would be the same in each recursive call.
sepp2k
+1  A: 

Why's the printf("%d") while it's supposed to print a float? Doesn't that display an integer making it always 0 for a float less than 0?

float recursion(float num)
{
    if( num==1.0f)
    {
        printf("1/1 = ");
        return 1.0f;
    }
    float inverse = 1.0f/num;
    printf("1/%.0f + ", num);
    return (inverse + recursion(--num));
}//recursion function ends

Here's the test code:

float num=5,sum=0;
float expected = 0;
for (int i = 1; i <= num; ++i)
{
    expected += 1.0f/i;
}
//input num
printf("Expected %f and got %f",expected, recursion(num));

Output:
1/5 + 1/4 + 1/3 + 1/2 + 1/1 = Expected 2.283334 and got 2.283334

Hope this helps.

Vite Falcon
can u help me with my code?
fahad
A: 

@fahad: Changes in your code has been commented in the code below:

float recursion2(float num,float sum)
{
    // int sum=0; every time u run it the sum is assigned 0
    if( num==1)
        // Vite Falcon: Needs to return sum + 1
        return sum + 1.0f;
    else
    {
        // Vite Falcon: This is not really necessary.
        //sum=sum+(1/num);
        float inverse = 1.0f/num;
        num--;
        // Vite Falcon: The new sum is returned by the recursive function and so
        // should be stored and returned.
        sum = recursion2(num,sum + inverse);
    }
    return sum;
}//recursion function ends

PS: Sorry I had to answer again because I don't know how to add multi-line code as a comment.

Vite Falcon