2

enter image description here

Here is the C++ program i wrote to solve the above series:

#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;

int factorial(int a)
{
    if (a > 1)
        return a * factorial(a - 1);
    else
        return 1;
}

float series(float x, int n, float b)
{
    if (abs(pow(x, n) / factorial(n)) < pow(10, -6) || abs(pow(x, n) / factorial(n)) == pow(10, -6)) { return b; }
    else return b = (pow(x, n) / factorial(n)) + series(x, n + 1, b);
}

int main()
{
    float x;
    cout << "Enter x: "<<endl;
    cin >> x;
    cout << "E^x = " << series(x,0,0);
    system("pause");
    return 0;
}

It works fine when abs(x) < 2 but when abs(x) >= 2 this error appears:

Unhandled exception at 0x00F02539 in 33b.exe: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x00F22FF8). occurred enter image description here

I want to know why does this happen and how can i fix it?

2 Answers2

3

Your problem is too deep recursion. Consider loop instead.

float series(float x)
{
    const float epsilon = 1e-6f;
    double error = 1;
    double res = 1.f;
    int iter = 1;
    while (abs(error) > epsilon) {
        error *= (x / iter++);
        res += error;
        cout << error << endl;
    }
    return res;
}

int main()
{
    cout << "E^x = " << series(3);
    system("pause");
    return 0;
}

To be clearer about what happens:

When you call a function inside another function, the context of the parent function is saved to make room for the new context. When you make millions of inception, the memory stack in charge to save these context is full and overflows.

This is a Stack Overflow.

Benjamin Barrois
  • 2,566
  • 13
  • 30
Yola
  • 18,496
  • 11
  • 65
  • 106
  • 1
    Yes, this clearly is the problem. Look at the small logo up right, you'll see an illustration of a stack overflow. – Benjamin Barrois Jan 29 '18 at 15:38
  • @BenjaminBarrois yes, funnily the question is about this. – Yola Jan 29 '18 at 15:39
  • @Yola Just asking, what does writing "double res = 1.f" mean for "res" (instead of, you know: "double res = 1") ? I'm new to C++. – Trần Đức Hiếu Jan 29 '18 at 16:19
  • 1
    @TrầnĐứcHiếu `1.` is `double` literal, `1.f` is `float` literal. I changed type to `double`, but forgot to change literal, correct would be `double res = 1.;`. And just `1` is literal for `int` – Yola Jan 29 '18 at 18:21
1
 #include <iostream>
 #include <cmath>
 #include <cstdlib>
 using namespace std;
 int factorial[200];
 int Factorial(int a)
 {    if(a>0){
     factorial[a]=a * factorial[a-1];
         return factorial[a];
    }
    else
    factorial[a]=1;
    return 1;

 }

 double series(double x, int n, double b)
 {   double temp=(abs(pow(x, n)) / Factorial(n));
     if (temp <= 0.000001) { return b; }
     else return (temp + series(x, n + 1, b));
 }

 int main()
 {
     float x;
     cout << "Enter x: "<<endl;
     cin >> x;
     cout << "E^x = " << series(x,0,0);
     system("pause");
     return 0;
 }

umm this solution is working. all i did was i took your code removed abs(pow(x, n) / factorial(n)) wherever its repeating and intialised to a new variable temp. then instead of < || == u can directly put <=. and rather than invoking a a function to calculate .000001 every time you could just give that value to reduce time further. however i believe that the reason why the code may not have worked is too much recursion. so for factorials i used dynamic programming to reduce its complexity. the above code is working perfectly fine.