It turns out that the program I have developed to do the Fibonacci sequence n times -value entered by the user- gives me the following erroneous result:
(139)bash: line 1: 22 Done printf '12' 23 Segmentation fault (core dumped) | ./program
I attach the code:
#include <iostream>
#include <string>
using namespace std;
int fibonacci(int n, int i);
int main(){
int n;
cin >> n;
for(int i=0; i<n; i++){
cout << fibonacci(n, i);
}
}
int fibonacci(int n, int i){
if (n == 0){
return 0;
}
else if (n == 1){
return 1;
}
else{
return fibonacci(n, i - 1) + fibonacci(n, i - 2);
}
}
The program gives you an exception because it enters an infinite loop and by not decreasing
n
the program it runs out of memory since it will always enter the last oneelse
and will never exit.Complementing the Chariot Comment I show you the implementation of the corrections that were mentioned before:
Greetings, I hope you serve.