Yes the base case is 1.
The base case is the simplest case and you know what the return value is for that case. This is the moment when your function returns a final element to the caller.
In your problem the known and simplest case is when n = 1, since all the sum in this case will end when n is equal to 1, in your problem we have that the sum of 1 is n/(1+n) => 1/(2) => 0.5.
So say we want the sum of 5 that would be (5/6)+(4/5)+(3/4)+(2/3)+(1/2) correct?
we may also think that the sum of 5 is: 5/(1+5) + sum of 4.
if we think that the sum of 4 is 4/(1+4) + the sum of 3 and so on then we would have to:
somatorio(5) = 5/(1+5) + somatorio(4)
somatorio(4) = 4/(1+4) + somatorio(3)
somatorio(3) = 3/(1+3) + somatorio(2)
somatorio(2) = 2/(1+2) + somatorio(1)
OPA! We know that the sum of 1 is 0.5 because it is our base case
so we would have:
somatorio(2) = 2/(1+2) + 0.5 => 1.1666...
then we know that somatorio(2) = 1.1666, and now we can calculate the sum of 3, of 4 and finally of 5.
I wrote an example in code of the algorithm described above to try to demonstrate how it would look.
# include <stdio.h>
/*Função recursiva (calcula o somatorio de n)*/
float somatorio(int n)
{
/*Veja que meu caso base é o somatório de 1
e retorno o resultado que eu ja sei somatorio(1) = 1*/
if (n == 1)
{
return 0.5;
}
/*Se não for meu caso base então eu
calculo o somatório do meu antecessor e somo o meu valor*/
else
{
return ((float) n/(1+n) + somatorio(n-1));
}
}
int main()
{
//Imprimo o resultado do somatorio de 5
printf("%f", somatorio(5));
return 0;
}
I hope I’ve helped!
I don’t understand what this division by 1 is, it doesn’t make sense and why it has to add
n´ com
n`. What sum is this? I think this code should have 2 lines, maybe one. See if this is it: http://ideone.com/Hcvn1D– Maniero
I then put an image with the program statement. Despite this, just below a user helped in the elaboration of the algorithm.
– Misael