Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

http://www.chegg.com/homework-help/C-Programming-7th-edition-chapter-5-problem-3

ID: 3595560 • Letter: H

Question

http://www.chegg.com/homework-help/C-Programming-7th-edition-chapter-5-problem-32PE-solution-9781285852751

The number is a mathematical constant that is the base of the natural logarithm (i.e., the only number whose natural logarithm is equal to one). Its value to nine decimal positions is = 2.718281827 and is the limit of (1 + 1/) as approaches infinity. Interestingly, the value of can also be approximated using the following expression: 2 + 1 1 + 1 2 + 2 3 + 3 1 + 1 + where n is a positive integer.

a) Write a C++ program that uses this expression to approximate the value of .

b) Test your program for = 3, 5, 10, and 50. Give an approximation of to fifteen decimal positions

Explanation / Answer

#include <iostream>
#include <iomanip>

using namespace std;

double calculateE(int input) {
double n = input;
double approxE = (n-1) + (n-1)/(n+n);;
for (int i = n-1; i>=2; i--) {
double term = (i-1) + (i-1)/approxE;
approxE = term;
}
approxE = 2 + 1/approxE;
return approxE;
}

int main()
{
cout << fixed << showpoint;
cout << setprecision(15);
cout << calculateE(3) << endl;
cout << calculateE(5) << endl;
cout << calculateE(10) << endl;
cout << calculateE(50) << endl;

return 0;
}

Sample run