a. The sum of a series can be computed as sum=x+x^2/2+x^3/3+x^4/4+x^5/5+⋯ ∞<x<∞ stopping when either n terms have been added or when the magnitude of a term in the series is less than 10-6, whichever comes first.
You are required to write a function, sumSeries, which given x, n returns the sum of the series. Use #include <cmath> and the abs function. Do not use the pow function.
1
Expert's answer
2018-03-20T08:43:35-0400
#include <cmath> double sumSeries(double x, int n) { double sum = 0; double nominator = 1; for (int i = 1; i <= n; ++i) { nominator *= x; double element = nominator/i; sum += element; if (std::abs(element) < 0.00001) break; } return sum; }
Comments
Leave a comment