Write a MAIN function and a FUNCTION to compute the sum of the below series.
(n!)/(2n+1)! (from n=1 to infinity)
Within the MAIN function:
. Read a variable EPSILON of type DOUBLE (desired accuracy) from
the standard input.
EPSILON is an extremely small positive number which can be anything
between 1E-06 (10^(-6)) and 1E-12 (10^(-12)).
. EPSILON value will be passed to the FUNCTION as an argument.
Within the FUNCTION:
. In a do-while loop:
. Continue adding up the terms until |Sn+1 - Sn| < EPSILON.
. Sn is the sum of the first n-terms.
. Sn+1 is the sum of the first (n+1)-terms.
When the desired accuracy EPSILON is reached print the SUM and the number
of TERMS added to the sum.
Thanks for now
Here is an example of a C++ program that includes a main function and a separate function to compute the sum of the series you described:
```cpp
#include <iostream>
#include <cmath>
using namespace std;
double computeSeriesSum(double epsilon) {
double sum = 0.0;
double sn = 0.0;
double snPlus1 = 0.0;
int n = 1; // starting from the first term
do {
sn = snPlus1; // update Sn for the next iteration
// calculate the (n!)/(2n+1)!
double term = 1.0;
for (int i = 1; i <= n; i++) {
term *= i;
}
for (int i = 1; i <= (2 * n + 1); i++) {
term /= i;
}
sum += term; // add the term to the sum
n++; // move to the next term
// calculate Sn+1 for the next iteration
snPlus1 = sn + term;
} while (abs(snPlus1 - sn) >= epsilon);
cout << "Sum: " << sum << endl;
cout << "Number of terms added: " << n << endl;
return sum;
}
int main() {
double epsilon;
cout << "Enter the desired accuracy (EPSILON): ";
cin >> epsilon;
computeSeriesSum(epsilon);
return 0;
}
```
Explanation:
1. The program includes the necessary header files `iostream` and `cmath` for input/output and mathematical operations.
2. The `computeSeriesSum` function takes the desired accuracy (`epsilon`) as an argument and returns the computed sum.
3. Inside the `computeSeriesSum` function, variables `sum`, `sn`, and `snPlus1` are defined to store the sum, current Sn, and next Sn+1.
4. The variable `n` is initialized to 1 to start from the first term of the series.
5. The do-while loop is used to calculate the series sum and check for the desired accuracy.
6. Inside the loop, the term (n!)/(2n+1)! is calculated using two nested for loops. The first loop calculates the value of `n!` (n factorial) and the second loop calculates the value of `(2n+1)!` ((2n+1) factorial). The term is then added to the `sum`.
7. The value of `n` is incremented after each iteration.
8. The current `sn` is updated with the value of `snPlus1` for the next iteration.
9. The loop continues until the absolute difference between `snPlus1` and `sn` is less than `epsilon`.
10. Finally, the computed sum and the number of terms added to the sum are printed.
11. In the `main` function, the user is prompted to enter the desired accuracy (`epsilon`).
12. The `computeSeriesSum` function is called with `epsilon` as the argument.
13. The program terminates after printing the sum and the number of terms added to the sum.