Little problem with acumulative function.

Do a recursive function in C that calculates S:
S = 2 + 5 + 10/3 + ...+ (1+n²/n)
n is determined by you in the beggining. Please make it with n = 5 or higher.Thanks
OK, so how far have you got?
People here are generaly happy to help where you are stuck, but you need to start.
I tried many different ways, but i guess this one is on the right way:

#include<stdio.h>
#include<math.h>
int somas(int);
int main()
{
int n;
float acm;
somas(n);
}
int somas(int x)
{
int n, b, c;
float soma1, acm;
printf("select the value for n: ");
scanf("%d", &n);
if ( n = 0)
{
printf("%f", acm);
return(0);
}
else
{
soma1 == (1+pow(n,2))/n;
acm += soma1;
n-1 == b;
b == n;
return((1+pow(n,2))/n);
}
}
consider "n == 0" instead of n = 0.
OK, A recursive function is one that calls itself.
You should move the code to select n out of the function and into main() (as you only want to do this once), and also the print for the output.
The function should concern itself only with the calculation.
The function will need to be of type float (or double) to return a floating point number.
You are also confusing '=' (assignment) and '==' (comparison).
So you should have if ( n == 0) as the check for a guard condition.
In the 'else' portion is where you put the recursive call.
You need to add the value for a single element (1+pow(n,1))/n to the result for (n-1) - which is the value of somas(n-1) and return that combined value.
The recursion functions in that when it calculates somas(n-1) it in turn calls somas((n-1)-1) and so on, until it reaches the guard condition (n=0).

I hope this helps - it is difficult to explain without simply writing the answer, which kinda defeats the purpose of you trying to solve it:-)
thanks a lot. I'm following each tip, i think i can do it. Ill reply if something goes wrong.
closed account (z05DSL3A)
A recusive function, is one that calls itself.

Pseudocode

1
2
3
4
5
6
7
8
9
10
11
12
Main Procedure
    Get Value from User
    result = doSum(value)
    display result
End Procedure
    
Procedure doSum(in) 
    IF in equals 0 
        return 0
    ELSE
        return (1 + in2/in) + doSum(in -1)
End Procedure


EDIT: Beaten to it
Last edited on
why can't I just acumulate the soma(n) and return only soma(n-1) and then acumulate again when it comes back? Is the acumulate only effective in the first turn?
And just to make it clear.. The Return command at thje end of the recursive function returns that value to MAIN or to the beggining of the recursive function?
closed account (z05DSL3A)
why can't I just acumulate the soma(n) and return only soma(n-1) and then acumulate again when it comes back? Is the acumulate only effective in the first turn?

I don't understand what you are asking here.

And just to make it clear.. The Return command at thje end of the recursive function returns that value to MAIN or to the beggining of the recursive function?

The return will return to where the function was called.

For simplicity let's look at the following Pseudocode:
1
2
3
4
5
6
Procedure doSum(in) 
    IF in equals 0 
        return 0
    ELSE
        return (in) + doSum(in -1)
End Procedure


If you call doSum with the value of 2, it will want to return 2+ doSum(2-1).
so doSum(1) is called and this time it wants to return 1 + doSum(1-1).
so doSum(0) is called, this time as in is 0 it returns 0 to the call doSum(1).
now doSum(1) can return 1 + 0 to doSum(2) and finally doSum(2) can return 2+1 to where ever called soSum(2) in the first place.

Dose that make sense?
Last question first, and add to what Grey Wolf has already said:
Your recursive function returns the value to which ever function called it. For example if n = 4
main() -> somas(4) which in turn calls somas(3) and so on
 
main() -> somas(4) -> somas(3) -> somas(2) -> somas(1) ->somas(0)

so when the somas(0) returns it returns its value to somas(1) and so on until somas(4) returns to main().
It's a bit like extending a slinky spring and then squashing it back together again. Don't forget the metaphoric slinky is being extended into memory and if n is too big, your memory will run out.
For your first question if my guess about what you asking is correct, you can do it without recursive calls but it's gets quite complicated having to add the results of various calls to somas() together. The code this way is much more straight forward.
Alright, it makes a lot more of sence now. Thank you all, and sorry you had to write a code to make it clear, this is the last time I copy a code.
Topic archived. No new replies allowed.