1. ## Recursion problem...help

Hi

I have this problem..

if n<=7, seq(n)=10

if n>7, seq(n)= 3*n+seq(n-1)-21

I must use recursion, iv'e made something like that but i have always segmentation fault.

Code:
```int seq(int n)
{
if(n<7) //under 7 i must use seq(10)
{
return(3*n+seq(n-1)-21);
}
else
{
return(3*n+seq(n-1)-21);
}
return 0;
}```

What's wrong with this??

2. Your if- and else-block are identical so it doesn't matter what value n has and your recursion is infinite.
I guess you should break out of the recursion when n reaches 7, aren't you?

Bye, Andreas

3. Hi Andreas;

No, when n > 7 i must use this formula (3*n+seq(n-1)-21);

if n <=7 (seq(n)=10)

I don't understand this like this.

Code:

Code:
```int calculaSequencia(int n)
{
if(n<7)
{
return 10;
}
else
{
return(3*n+seq(n-1)-21);
}
return 0;
}```
The exercise say:

Implement with recursion, the function CalculaSequencia to calculate the n's of the sequency.

4. But was should the function return as a whole?
For example what's the solution for n=7, n=8, n=9?

Bye, Andreas

5. Well, for 8, the program gives me 13, 9 gives me 19

Iv'e made some changes, but my doubt is:

if n<=7, seq(n)=10

Code:
```int calculaSequencia(int n)
{
if(n<7)
{
return 10;
}
else
{
return((3*n-21)+calculaSequencia(n-1));
}
return 0;
}```
It's hard to believe tha the solution is return 10, it's too easy.

6. All problems are easy as soon as you understand them :-)

Every recursion needs an end point where it returns a fixed value to its caller (one level above in the call stack).

And you don't need the return 0 at the end of the function (the function will never reach it).

Bye, Andreas

7. So unless the return 0, the function is correct?

Thanks

8. If I didn't misinterpret your problem description, yes.
Do you get the expected results?

Bye, Andreas

9. Hi
Andreas;;

I dont' have any result given, just the definition to use in the function.

I think it should be ok then.

Thanks

10. What value do you get when you pass in n=7? Look closely at your comparison.

11. I forgot that is (if <=7), i just put (if n<7).

My mistake, sorry