Your program has infinite recursion - in other words, your recursive function just keeps on calling itself until you eventually overflow the stack and your program crashes.
You need to write a check into your recursive function which prevents it from calling itself ad-infinitum.
If you are returning the summation of m(n) then you'll want to carry the current sum along
also you need to decrement and check the argument that sets the upper bounds of the summation.
#include <iostream>
usingnamespace std;
double m(int n, double s = 0.0)
{
double sum = s + (n/((2.0*n)+1.0));
// ternary operator acts like if..then..else
// (--n) if we decrement n, is it still > 0?
// ? m(n,sum) if n is still > 0, recurse
// : sum if n is zero just return the sum
return (--n) ? m(n,sum) : sum;
}
int main()
{
int n;
cout<<"Enter a value: "<<endl;
cin>>n;
if (n<1)
{
return 0;
} else {
double summation = m(n);
cout << "Summation for m(n) where n / (2n + 1) : " << summation << endl;
return summation; // though this is odd since it will only return the integer portion
}
}
In simplest terms, recursive functions need a "base case". This is the case that causes recursive functions to stop recursing. This can be in the form of an if statement at the beginning of the function, or a ternary return operation as Texan40 has shown, etc.