I would like you to create code to calculate e using 100 terms of the power seri
ID: 3746909 • Letter: I
Question
I would like you to create code to calculate e using 100 terms of the power series expansion. You will compare your answer to the actual value of e given 50 decimal places as mention in part 3 However, there is a little bit of a twist in this last part. First, I would like you to calculate a cumulative sum in order from the largest term to the smallest term in the power series. Then, I would then like for you to sum the values in the reverse order. You might consider putting each term in the power series in an array with 100 elements. You can then use a sum term that starts summing from the beginning of the array for the forward summation. You can compare this answer to the sum starting at the end of the array. In theory you would think it should not make a difference which order you sum the values, but you should see a difference in the result. Please make sure your program shows the difference between the two. Also, please do this with float types and then double types. Why do you think there is a difference between these the forward and backward summations? In your Labl directory, please call your source code lablpart4.cc. Sample output of your program should look like: forward approx- forward % error-your value> backward approx- backward %error-your value> forward approx- forward % error-your value> backward approx = your value> backward %error-your value>Explanation / Answer
Given below is the code for the question. I have first used the float datatype and commented out the code using double datatype. You can run the program second time by commenting the float variables and uncommenting the double variables
Please do rate the answer if it was helpful. Thank you
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main(){
float terms[100];
float forwardSum = 0, backwardSum = 0;
float forwardErr = 0, backwardErr = 0;
float factorial = 1;
float actualValue;
/*
double terms[100];
double forwardSum = 0, backwardSum = 0;
double forwardErr = 0, backwardErr = 0;
double factorial = 1;
double actualValue;*/
//store the 100 terms in the array
terms[0] = 1.0;
for(int i = 1; i < 100; i++){
factorial *= i;
terms[i] = 1.0/factorial;
}
//perform forward sum
for(int i = 0; i < 100; i++)
forwardSum += terms[i];
//perform backward sum
for(int i = 99; i >= 0; i--)
backwardSum += terms[i];
actualValue = exp(1.0);
forwardErr = abs(forwardSum - actualValue) / actualValue * 100.0;
backwardErr = abs(backwardSum - actualValue) / actualValue * 100.0;
cout << setprecision(50);
cout << setw(20) << "actual value = " << actualValue << endl;
cout << setw(20) << "forward approx = " << forwardSum << endl;
cout << setw(20) << "forward %error = " << forwardErr << endl;
cout << setw(20) << "backward approx = " << backwardSum << endl;
cout << setw(20) << "backward %error = " << backwardErr << endl;
}
output (for float):
---------
actual value = 2.71828174591064453125
forward approx = 2.7182819843292236328125
forward %error = 8.7709295257809571921825408935546875e-06
backward approx = 2.71828174591064453125
backward %error = 0
output (for double):
---------
actual value = 2.7182818284590450907955982984276488423347473144531
forward approx = 2.7182818284590455348848081484902650117874145507812
forward %error = 1.6337129034990842759171673612798189354302670628361e-14
backward approx = 2.7182818284590450907955982984276488423347473144531
backward %error = 0