Why do I get a segmentation fault in my recursive function. It happens every time i call it when a value greater than 4 as a parameter
#include <iostream>
#include <limits>
using namespace std;
int printSeries(int n){
if(n==1){
return 1;
}
else if( n==2){
return 2;
}
else if( n==3){
return 3;
}
else if( n==4){
return printSeries(1) + printSeries(2) + printSeries(3);
}
else{
return printSeries(n-3) + printSeries((n-2) + printSeries(n-1));
}
}
int main(){
//double infinity = numeric_limits<double>::max();
for(int i=1; i<=10; i++){
cout << printSeries(i) << endl;
}
return 0;
}
This works fine, but i'm not sure that returns the correct result:
return printSeries(n-3) + printSeries(n-2) + printSeries(n-1);